/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2024. All rights reserved.
 * Description: 与AI设备(D芯片)相关dnn,runtime,drv等接口适配
 * Author: ligang
 * Create: 2022-03-09
 */
#include <unordered_map>
#include <slog.h>
#include <slog_api.h>
#include <securec.h>
#include <runtime/dev.h>

#include "runtime/base.h"
#include "runtime/stream.h"
#include "runtime/rt_error_codes.h"
#include "runtime/context.h"
#include "runtime/event.h"
#include "driver/ascend_hal.h"
#include "externalinput_pub.h"
#include "log.h"
#include "adapter_rts.h"
#include "sal_pub.h"
#include "dlrts_function.h"
#include "adapter_error_manager.h"
#include "device_capacity.h"

using namespace hccl;
using namespace std;

#define CHK_RT_RET(call)                                 \
    do {                                                 \
        s32 ret = call;                                  \
        if (ret != 0) {                                  \
            HCCL_ERROR("call trace: ret -> %d", ret);    \
            return HCCL_E_RUNTIME;                       \
        }                                                \
    } while (0)

#define REPLACE_NOTIFY_WITH_EVENT(notify, event)         \
    do {                                                 \
        s32 ret = 0;                                     \
        if (ret != 0) {                                  \
            CHK_RT_RET(event);                           \
        } else {                                         \
            CHK_RT_RET(notify);                          \
        }                                                \
    } while (0)

constexpr char RTS_SO_NAME[] = "libruntime.so";
constexpr u32 H2D_COPY_FLAG = 0; // hccl中都需要runtime做args copy默认都设置为0
DlRtsFunction<RTS_SO_NAME> g_dlRts;

namespace {
    const char RT_SET_DEVICE[] = "rtSetDevice";
    const char RT_RESET_DEVICE[] = "rtDeviceReset";
    const char RT_MEMCPY[] = "rtMemcpy";
    const char RT_MALLOC_HOST[] = "rtMallocHost";
    const char RT_POINTER_GET_ATTR[] = "rtPointerGetAttributes";
    const char RT_FREE_HOST[] = "rtFreeHost";
    const char RT_IPC_SET_MEM_NAME[] = "rtIpcSetMemoryName";
    const char RT_SET_IPC_MEM_PID[] = "rtSetIpcMemPid";
    const char RT_IPC_OPEN_MEM[] = "rtIpcOpenMemory";
    const char RT_IPC_CLOSE_MEM[] = "rtIpcCloseMemory";
    const char RT_DESTORY_MEM_NAME[] = "rtIpcDestroyMemoryName";
    const char RT_CTX_CREATE[] = "rtCtxCreate";
    const char RT_CTX_DESTROY[] = "rtCtxDestroy";
    const char RT_CTX_GET_CURRENT[] = "rtCtxGetCurrent";
    const char RT_CTX_SET_CURRENT[] = "rtCtxSetCurrent";
    const char RT_GET_LOGICID_BY_PHYID[] = "rtGetDeviceIndexByPhyId";
    const char RT_RDMA_DB_SEND[] = "rtRDMADBSend";
    constexpr char RT_FREE[] = "rtFree";
    constexpr char RT_MALLOC[] = "rtMalloc";
    constexpr char RT_GET_SOC_VERSION[] = "rtGetSocVersion";
    constexpr char RT_GET_PAIR_PHY_DEVICES_INFO[] = "rtGetPairPhyDevicesInfo";
}
u32 g_stubDeviceId = 0;
static std::unordered_map<s32, s64> g_deviceChipIdMap; // 记录 devLogID 和 chipID 的关系，避免重复查询
bool g_workModeAicpu = false;                          // AI场景下aicpu工作模式
DevType g_localDeviceType = DevType::DEV_TYPE_COUNT;
s32 g_localDeviceLogicId = INVALID_INT;
rtFloatOverflowMode_t g_deviceSatMode = RT_OVERFLOW_MODE_UNDEF;
namespace {
static thread_local s32 g_deviceLogicId = INVALID_INT;
static thread_local u32 g_devicePhyId = INVALID_UINT;
static thread_local DevType g_deviceType = DevType::DEV_TYPE_COUNT;
}
static constexpr int32_t RT_NOT_SUPPORT = 207000;

#if T_DESC("Device管理", true)
HcclResult hrtThreadExchangeCaptureMode(rtStreamCaptureMode *mode);
HcclResult hrtGetIsHeterogenous(u32 &heterogenous)
{
#ifndef HCCD
    rtError_t ret = rtGetIsHeterogenous((reinterpret_cast<int32_t *>(&heterogenous)));

    HCCL_DEBUG("Call rtGetIsHeterogenous, return value[%d], para: heterogenous[%u].", ret, heterogenous);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][GetIsHeterogenous]errNo[0x%016llx] "
        "rtGetIsHeterogenous heterogenous fail, return[%d], para:heterogenous[%u]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, heterogenous), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetIsHeterogenous]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
};

HcclResult hrtGetDeviceCount(s32 *count)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(count);

    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType == DevType::DEV_TYPE_NOSOC) {
        *count = 0;
        return HCCL_SUCCESS;
    }

    rtError_t ret = rtGetDeviceCount(count);

    HCCL_DEBUG("Call rtGetDeviceCount, return value[%d], para: count[%d].", ret, *count);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][DeviceCount]errNo[0x%016llx] rtGet device count fail, "\
        "return[%d], para:count[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *count), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetDeviceCount]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
};

/* 设定当前线程操作的目标设备编号 */
HcclResult hrtSetDevice(s32 deviceLogicId)
{
#ifndef HCCD
    rtError_t ret = rtSetDevice(deviceLogicId);
    HCCL_DEBUG("Call rtSetDevice, return value[%d], para: device_id[%d], g_deviceLogicId = %d.",
        ret, deviceLogicId, g_deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][Device]errNo[0x%016llx] rtSet device fail, return[%d], "\
        "para:deviceLogicId[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, deviceLogicId), HCCL_E_RUNTIME);
    g_deviceLogicId = INVALID_INT;
    return HCCL_SUCCESS;
#else
    static auto funcPtr = (rtError_t(*)(int32_t))g_dlRts.Handle<RT_SET_DEVICE>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(deviceLogicId);
    HCCL_DEBUG("Call rtSetDevice, return value[%d], para: device_id[%d], g_deviceLogicId = %d.",
        ret, deviceLogicId, g_deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][Device]errNo[0x%016llx] rtSet device fail, return[%d], "\
        "para:deviceLogicId[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, deviceLogicId), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#endif
}

/* 释放当前线程操作的目标设备编号,释放前必须先释放设备资源 */
HcclResult hrtResetDevice(s32 deviceLogicId)
{
#ifndef HCCD
    rtError_t ret = rtDeviceReset(deviceLogicId);

    HCCL_DEBUG("Call rtDeviceReset, return value[%d], para: device_id[%d].", ret, deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Reset][Device]errNo[0x%016llx] rtReset device fail, return[%d], "\
        "para: deviceLogicId[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, deviceLogicId), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    static auto funcPtr = (rtError_t(*)(int32_t))g_dlRts.Handle<RT_RESET_DEVICE>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(deviceLogicId);

    HCCL_DEBUG("Call rtDeviceReset, return value[%d], para: device_id[%d].", ret, deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Reset][Device]errNo[0x%016llx] rtReset device fail, return[%d], "\
        "para: deviceLogicId[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, deviceLogicId), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#endif
}

HcclResult stubSetDevice(u32 deviceLogicId)
{
    g_stubDeviceId = deviceLogicId;
    return HCCL_SUCCESS;
}

HcclResult hrtGetDeviceRefresh(s32 *deviceLogicId)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(deviceLogicId);

    rtError_t ret = 0;
    ret = rtGetDevice(deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][DeviceRefresh]errNo[0x%016llx] rtGet device fail, "\
        "please make sure that device is set. return[%d], para:deviceLogicId[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *deviceLogicId), HCCL_E_RUNTIME);
    g_deviceLogicId = *deviceLogicId;
    HCCL_INFO("[hrtGetDeviceRefresh]deviceLogicId[%d]", *deviceLogicId);
    return HCCL_SUCCESS;
#else
    HCCL_WARNING("[hrtGetDeviceRefresh]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSetlocalDevice(s32 deviceLogicId)
{
#ifdef HCCD
    g_localDeviceLogicId = deviceLogicId;
#endif
    return HCCL_SUCCESS;
}

/* 查询当前线程目前操作的目标设备编号 */
#ifdef __cplusplus
extern "C" {
#endif
HcclResult __hrtGetDevice(s32 *deviceLogicId)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(deviceLogicId);

    if (LIKELY(g_deviceLogicId != INVALID_INT)) {
        *deviceLogicId = g_deviceLogicId;
        return HCCL_SUCCESS;
    }
    rtError_t ret = 0;
    ret = rtGetDevice(deviceLogicId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_WARNING("[Get][Device]errNo[0x%016llx] rtGet device fail, "\
        "please make sure that device is set. return[%d], para:deviceLogicId[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *deviceLogicId), HCCL_E_RUNTIME);
    g_deviceLogicId = *deviceLogicId;
    HCCL_INFO("[hrtGetDevice]deviceLogicId[%d]", *deviceLogicId);
    return HCCL_SUCCESS;
#else
    if (g_workModeAicpu) {
        HCCL_DEBUG("[hrtGetDevice]Device logicId = %d.", g_localDeviceLogicId);
        *deviceLogicId = g_localDeviceLogicId;
        return HCCL_SUCCESS;
    }
    *deviceLogicId = 0;
    HCCL_WARNING("[hrtGetDevice]Does does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
weak_alias(__hrtGetDevice, hrtGetDevice);
#ifdef __cplusplus
}  // extern "C"
#endif


HcclResult hrtCtxCreate(rtContext_t *createCtx, uint32_t flags, int32_t devId)
{
    CHK_PTR_NULL(createCtx);
#ifndef HCCD

    rtError_t ret = rtCtxCreate(createCtx, flags, devId);
#else
    static auto funcPtr = (rtError_t(*)(rtContext_t *, uint32_t, int32_t))g_dlRts.Handle<RT_CTX_CREATE>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(createCtx, flags, devId);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE && ret != ACL_ERROR_RT_CONTEXT_NULL, HCCL_ERROR(
        "[Get][Device]errNo[0x%016llx] rtCtxCreate fail, return[%d], para:flags[%u], devId[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, flags, devId), HCCL_E_RUNTIME);
    HCCL_INFO("[hrtCtxCreate]deviceLogicId[%d]", devId);
    return HCCL_SUCCESS;
}

HcclResult hrtCtxDestroy(rtContext_t destroyCtx)
{
    CHK_PTR_NULL(destroyCtx);
#ifndef HCCD
    rtError_t ret = rtCtxDestroy(destroyCtx);
#else
    static auto funcPtr = (rtError_t(*)(rtContext_t))g_dlRts.Handle<RT_CTX_DESTROY>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(destroyCtx);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE && ret != ACL_ERROR_RT_CONTEXT_NULL, HCCL_ERROR(
        "[Get][Device]errNo[0x%016llx] rtCtxDestroy fail,  return[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
}

HcclResult hrtCtxGetCurrent(HcclRtContext *ctx)
{
    CHK_PTR_NULL(ctx);
#ifndef HCCD
    rtError_t ret = rtCtxGetCurrent(ctx);
#else
    static auto funcPtr = (rtError_t(*)(rtContext_t*))g_dlRts.Handle<RT_CTX_GET_CURRENT>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(ctx);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE && ret != ACL_ERROR_RT_CONTEXT_NULL, HCCL_ERROR(
        "[Get][Device]errNo[0x%016llx] rtCtxGetCurrent fail, "\
        "please make sure that DIE is set. return[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    if (ret == ACL_ERROR_RT_CONTEXT_NULL) {
        *ctx = nullptr;
    }
    return HCCL_SUCCESS;
}

HcclResult hrtCtxSetCurrent(HcclRtContext ctx)
{
    CHK_PTR_NULL(ctx);
#ifndef HCCD
    rtError_t ret = rtCtxSetCurrent(ctx);
#else
    static auto funcPtr = (rtError_t(*)(rtContext_t))g_dlRts.Handle<RT_CTX_SET_CURRENT>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(ctx);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][Device]errNo[0x%016llx] rtCtxSetCurrent fail, "\
        "please make sure that DIE is set. return[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    HCCL_INFO("[hrtCtxSetCurrent] success");
    return HCCL_SUCCESS;
}

#ifdef __cplusplus
extern "C" {
#endif
HcclResult __hrtGetDevicePhyIdByIndex(u32 deviceLogicId, u32 &devicePhyId, bool isRefresh)
{
#ifndef HCCD
    if (LIKELY(g_devicePhyId != INVALID_UINT) && (!isRefresh)) {
        devicePhyId = g_devicePhyId;
        return HCCL_SUCCESS;
    }

    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType == DevType::DEV_TYPE_NOSOC) {
        devicePhyId = 0;
        g_devicePhyId = 0;
        return HCCL_SUCCESS;
    }

    rtError_t ret = rtGetDevicePhyIdByIndex(deviceLogicId, &devicePhyId);
    if (ret != RT_ERROR_NONE) {
        HCCL_ERROR("[Get][DevicePhyId]errNo[0x%016llx] rtGet device PhyId by index failed, return[%d], "\
            "para: devIndex[%u], phyId[%u]", HCCL_ERROR_CODE(HCCL_E_DRV), ret, deviceLogicId, devicePhyId);
        return HCCL_E_RUNTIME;
    }
    g_devicePhyId = devicePhyId;
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetDevicePhyIdByIndex]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
weak_alias(__hrtGetDevicePhyIdByIndex, hrtGetDevicePhyIdByIndex);
#ifdef __cplusplus
}  // extern "C"
#endif

HcclResult hrtGetDeviceIndexByPhyId(u32 devicePhyId, u32 &deviceLogicId)
{
#ifndef HCCD
    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType == DevType::DEV_TYPE_NOSOC) {
        deviceLogicId = 0;
        return HCCL_SUCCESS;
    }

    rtError_t ret = rtGetDeviceIndexByPhyId(devicePhyId, &deviceLogicId);
#else
    static auto funcPtr = (rtError_t(*)(u32, u32*))g_dlRts.Handle<RT_GET_LOGICID_BY_PHYID>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(devicePhyId, &deviceLogicId);
#endif
    if (ret != RT_ERROR_NONE) {
        HCCL_ERROR("[Get][DeviceIndex]errNo[0x%016llx] rtGet device logicid by PhyId failed, return[%d], "\
            "para: phyId[%u], devIndex[%u]", HCCL_ERROR_CODE(HCCL_E_DRV), ret, devicePhyId, deviceLogicId);
        return HCCL_E_RUNTIME;
    }
    return HCCL_SUCCESS;
};

HcclResult hrtGetPhyDeviceInfo(u32 devicePhysicId, s32 moduleType, s32 infoType, s64 &value)
{
#ifndef HCCD
    rtError_t rtRet = rtGetPhyDeviceInfo(devicePhysicId, moduleType, infoType, reinterpret_cast<int64_t *>(&value));
    HCCL_DEBUG("rt get device info, return[%d], para: devicePhysicId[%u], moduleType[%d], infoType[%d], "\
        "value[%d].", rtRet, devicePhysicId, moduleType, infoType, value);
    CHK_PRT_RET(rtRet != RT_ERROR_NONE, HCCL_ERROR("[Get][DeviceInfo]errNo[0x%016llx] rt get pair devices "\
        "info failed, return[%d], para:devicePhysicId[%u], moduleType[%d], infoType[%d], value[%d].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, devicePhysicId, moduleType, infoType, value), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetPhyDeviceInfo]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetVisibleDeviceIdByLogicDeviceId(const int32_t logicDeviceId, int32_t *visibleDeviceId)
{
#ifndef HCCD
    rtError_t rtRet = rtGetVisibleDeviceIdByLogicDeviceId(logicDeviceId, visibleDeviceId);
    HCCL_DEBUG("rt get VisibleDeviceId by LogicDeviceId, return[%d], para: logicDeviceId[%u],"\
        "visibleDeviceId[%d], ", rtRet, logicDeviceId, *visibleDeviceId);
    CHK_PRT_RET(rtRet != RT_ERROR_NONE, HCCL_ERROR("[Get][VisibleDeviceId]errNo[0x%016llx] "\
        "rt get visible DeviceId failed, return[%d], para: logicDeviceId[%d], visibleDeviceId[%d].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, logicDeviceId, *visibleDeviceId), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetVisibleDeviceIdByLogicDeviceId]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetSocVer(s8 *chipVer, const u32 size)
{
#ifndef HCCD
    CHK_PTR_NULL(chipVer);

    rtError_t rtRet = rtGetSocVersion(reinterpret_cast<char *>(chipVer), size);
    CHK_PRT_RET((rtRet != RT_ERROR_NONE), HCCL_ERROR("[Get][SocVer]errNo[0x%016llx] rtGet deviceVer failed, "\
        "return value[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetSocVer]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

// 获取芯片类型
#ifdef __cplusplus
extern "C" {
#endif
const std::unordered_map<std::string, DevType> SOC_VER_CONVERT{
    {"Ascend310P1", DevType::DEV_TYPE_310P1},
    {"Ascend310P3", DevType::DEV_TYPE_310P3},
    {"Ascend310P5", DevType::DEV_TYPE_310P3},
    {"Ascend310P7", DevType::DEV_TYPE_310P3},
    {"Ascend910", DevType::DEV_TYPE_910},
    {"Ascend910A", DevType::DEV_TYPE_910},
    {"Ascend910B", DevType::DEV_TYPE_910},
    {"Ascend910ProA", DevType::DEV_TYPE_910},
    {"Ascend910ProB", DevType::DEV_TYPE_910},
    {"Ascend910PremiumA", DevType::DEV_TYPE_910},
    {"Ascend910B1", DevType::DEV_TYPE_910B},
    {"Ascend910B2", DevType::DEV_TYPE_910B},
    {"Ascend910B2C", DevType::DEV_TYPE_910B},
    {"Ascend910B3", DevType::DEV_TYPE_910B},
    {"Ascend910B4", DevType::DEV_TYPE_910B},
    {"Ascend910B4-1", DevType::DEV_TYPE_910B},
    {"Ascend910_9391", DevType::DEV_TYPE_910_93},
    {"Ascend910_9381", DevType::DEV_TYPE_910_93},
    {"Ascend910_9392", DevType::DEV_TYPE_910_93},   // Ascend910_9392、Ascend910_9382为预留类型，当前版本暂不支持，待跟随后续版本节奏交付
    {"Ascend910_9382", DevType::DEV_TYPE_910_93},
    {"Ascend910_9372", DevType::DEV_TYPE_910_93},
    {"Ascend910_9362", DevType::DEV_TYPE_910_93},
    {"nosoc", DevType::DEV_TYPE_NOSOC}};

HcclResult hrtGetDeviceTypeBySocVersion(std::string &socVersion, DevType &devType)
{
    devType = DevType::DEV_TYPE_COUNT;
    auto iter = SOC_VER_CONVERT.find(socVersion);
    if (iter == SOC_VER_CONVERT.end()) {
        HCCL_ERROR("[Get][DeviceType]errNo[0x%016llx] rtGetSocVersion get illegal chipver, chip_ver[%s].",
            HCCL_ERROR_CODE(HCCL_E_RUNTIME), socVersion.c_str());
        return HCCL_E_RUNTIME;
    }
    devType = iter->second;
    return HCCL_SUCCESS;
}
HcclResult hrtSetWorkModeAicpu(bool workModeAicpu)
{
#ifdef HCCD
    g_workModeAicpu = workModeAicpu;
#endif
    HCCL_INFO("[Set][hrtSetWorkModeAicpu]work mode aicpu[%u]", g_workModeAicpu);
    return HCCL_SUCCESS;
}

HcclResult hrtSetlocalDeviceType(DevType devType)
{
#ifdef HCCD
    g_localDeviceType = devType;
#endif
    return HCCL_SUCCESS;
}

HcclResult __hrtGetDeviceType(DevType &devType)
{
    s8 targetChipVer[CHIP_VERSION_MAX_LEN] = {0};

    HCCL_DEBUG("[hrtGetDeviceType]g_deviceType = %d.", static_cast<s32>(g_deviceType));
    if (LIKELY((g_deviceType != DevType::DEV_TYPE_COUNT))) {
        devType = g_deviceType;
        return HCCL_SUCCESS;
    }
#ifndef HCCD
    CHK_RET(hrtGetSocVer(targetChipVer, CHIP_VERSION_MAX_LEN));
#else
    if (g_workModeAicpu) {
        HCCL_DEBUG("[hrtGetDeviceType]DeviceType = %d.", static_cast<s32>(g_localDeviceType));
        devType = g_localDeviceType;
        return HCCL_SUCCESS;
    }

    static auto funcPtr = (rtError_t(*)(char *, const uint32_t))g_dlRts.Handle<RT_GET_SOC_VERSION>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(reinterpret_cast<char *>(targetChipVer), CHIP_VERSION_MAX_LEN);
    CHK_PRT_RET((ret != RT_ERROR_NONE), HCCL_ERROR("[hrtGetDeviceType]errNo[0x%016llx] rtGetSocVerfailed, return[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
#endif
    auto iter = SOC_VER_CONVERT.find(reinterpret_cast<char *>(targetChipVer));
    if (iter == SOC_VER_CONVERT.end()) {
        HCCL_ERROR("[Get][DeviceType]errNo[0x%016llx] rtGetSocVersion get illegal chipver, chip_ver[%s].", \
            HCCL_ERROR_CODE(HCCL_E_RUNTIME), targetChipVer);
        return HCCL_E_RUNTIME;
    }
    devType = iter->second;
    g_deviceType = devType;
    return HCCL_SUCCESS;
}
weak_alias(__hrtGetDeviceType, hrtGetDeviceType);
#ifdef __cplusplus
}  // extern "C"
#endif
HcclResult hrtGetDeviceMode(rtDeviceMode *deviceMode)
{
#ifndef HCCD
    CHK_PTR_NULL(deviceMode);
    rtError_t ret = rtGetDeviceMode(deviceMode);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtGetDeviceMode] rtGet device mode failed"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetDeviceMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

#endif

#if T_DESC("DeviceMemory管理", true)

HcclResult HrtDevFree(void *devPtr)
{
    CHK_PTR_NULL(devPtr);

    static auto funcPtr = (rtError_t(*)(void *))g_dlRts.Handle<RT_FREE>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(devPtr);

    HCCL_DEBUG("Call rtFree, ret[%d], devPtr[%llu]", ret, hash<void *>{}(devPtr));
    CHK_PRT_RET((ret != RT_ERROR_NONE), HCCL_ERROR("[Free][Mem]errNo[0x%016llx] rtFree failed, return[%d]"
        ", para: devPtrAddr[%llu].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, hash<void *>{}(devPtr)), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
}

HcclResult HrtDevMalloc(void **devPtr, u64 size)
{
    CHK_PTR_NULL(devPtr);

    static auto funcPtr = (rtError_t(*)(void **, uint64_t, rtMemType_t, const uint16_t))g_dlRts.Handle<RT_MALLOC>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(devPtr, size, RT_MEMORY_HBM, HCCL);

    HCCL_DEBUG("Call rtMalloc, ret[%d], devPtr[%llu], size[%llu Byte]", ret, hash<void *>{}(devPtr), size);
    CHK_PRT_RET((ret != RT_ERROR_NONE || *devPtr == nullptr), HCCL_ERROR("[Malloc][Mem]errNo[0x%016llx] rtMalloc "
        "failed, return[%d], para: devPtrAddr[%llu], size[%llu Byte].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret,
        hash<void *>{}(*devPtr), size), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
}

HcclResult hrtMalloc(void **devPtr, u64 size, bool level2Address)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(devPtr);

    DevType devType;
    CHK_RET(hrtGetDeviceType(devType));

    rtError_t rtRet = 0;
    if (Is310PDevice()) {
        rtMemType_t memType;
        if (devType == DevType::DEV_TYPE_310P3 || devType == DevType::DEV_TYPE_310P1) {
            if (level2Address) { // 310P二级地址刷新时申请内存类型为：RT_MEMORY_TS
                memType = RT_MEMORY_TS;
            } else {
                memType = RT_MEMORY_DDR;
            }
        } else {
            memType = RT_MEMORY_HBM;
        }
        rtRet = rtMalloc(devPtr, size, memType, HCCL);
        HCCL_DEBUG("Call rtMalloc, return value[%d] size[%llu Byte] level2Address[%u], moudleId: HCCL.",
            rtRet, size, level2Address);
    } else {
        if (devType == DevType::DEV_TYPE_310P3) {
            if (level2Address) { // 310P二级地址刷新时申请内存类型为：RT_MEMORY_TS
                HCCL_INFO("Call rtMalloc, para: MEMORY_DDR, level2Address is true, dev_ptr[%p], size[%llu Byte], "\
                    "moudleId: HCCL.", *devPtr, size);
                rtRet = rtMalloc(devPtr, size, RT_MEMORY_TS, HCCL);
                HCCL_DEBUG("return value[%d]", rtRet);
            } else {
                rtRet = rtMalloc(devPtr, size, RT_MEMORY_P2P_DDR, HCCL);
                HCCL_DEBUG("Call rtMalloc, return value[%d], para: MEMORY_DDR, dev_ptr[%p], size[%llu Byte], "
                    "moudleId: HCCL.", rtRet, *devPtr, size);
            }
        } else if (devType == DevType::DEV_TYPE_310P1) {
            rtRet = rtMalloc(devPtr, size, RT_MEMORY_DDR, HCCL);
            HCCL_DEBUG("Call rtMalloc, return value[%d], para: MEMORY_DDR, dev_ptr[%p], size[%llu Byte], moudleId: HCCL.",
                rtRet, *devPtr, size);
        } else {
            rtRet = rtMalloc(devPtr, size, RT_MEMORY_P2P_HBM, HCCL);
            HCCL_DEBUG("Call rtMalloc, return value[%d], para: P2P_HBM, dev_ptr[%p], size[%llu Byte], moudleId: HCCL.",
                rtRet, *devPtr, size);
        }
    }

    RPT_ENV_ERR((rtRet != RT_ERROR_NONE), "EI0007", std::vector<std::string>({"resource_type", "resource_info"}), \
        std::vector<std::string>({"DeviceMemory", std::string("size:") + std::to_string(size)}));

    CHK_PRT_RET((rtRet != RT_ERROR_NONE), HCCL_ERROR("[Malloc][Mem]errNo[0x%016llx] rtMalloc failed, "\
        "return[%d], para: devPtrAddr[%p], size[%llu Byte].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, *devPtr, size),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    // 参数有效性检查
    CHK_PTR_NULL(devPtr);

    *devPtr = malloc(size);
    CHK_PTR_NULL(*devPtr);
    return HCCL_SUCCESS;
#endif
}

HcclResult hrtMemSyncCopy(void *dst, uint64_t destMax, const void *src, uint64_t count, HcclRtMemcpyKind kind)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    rtStreamCaptureMode mode = rtStreamCaptureMode::RT_STREAM_CAPTURE_MODE_RELAXED;
    HcclResult hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemSyncCopy] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
    CHK_PRT_RET(count == 0, HCCL_WARNING("[hrtMemSyncCopy] count is zero"), HCCL_SUCCESS);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
    rtError_t ret = rtMemcpy(dst, destMax, src, count, rtKind);

    HCCL_DEBUG("Call rtMemcpy, return value[%d], dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu]",
        ret, dst, destMax, src, count);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[SyncCopy][Mem]errNo[0x%016llx] rtMemcpy failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], rtKind[%d].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, rtKind), HCCL_E_RUNTIME);
    hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemSyncCopy] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemSyncCopy]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemSyncCopyEx(void *dst, uint64_t destMax, const void *src, uint64_t count, HcclRtMemcpyKind kind)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);

    CHK_PRT_RET(count == 0, HCCL_WARNING("[hrtMemSyncCopyEx] count is zero"), HCCL_SUCCESS);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
    rtError_t ret = rtMemcpyEx(dst, destMax, src, count, rtKind);

    HCCL_DEBUG("Call rtMemcpyEx, return value[%d], dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu]",
        ret, dst, destMax, src, count);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[SyncCopy][Mem]errNo[0x%016llx] rtMemcpyEx failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], rtKind[%d].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, rtKind), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemSyncCopyEx]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtFree(void *devPtr)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(devPtr);
    rtError_t ret = rtFree(devPtr);

    HCCL_DEBUG("Call rtFree, return value[%d], para: dev_ptr[%p].", ret, devPtr);
    CHK_PRT_RET((ret != RT_ERROR_NONE), HCCL_ERROR("[Free][Mem]errNo[0x%016llx] rtFree failed, "\
        "return[%d], para: devPtrAddr[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, devPtr), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    CHK_PTR_NULL(devPtr);
    free(devPtr);

    return HCCL_SUCCESS;
#endif
}

HcclResult hrtMemcpyAddrAsync(void *dst, uint64_t destMax, uint64_t destOffset, const void *src, uint64_t count,
    uint64_t srcOffset, rtStream_t stream)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    CHK_PTR_NULL(stream);

    if (count == 0) {
        HCCL_WARNING("Call hrtMemcpyAddrAsync, count [%d]", count);
        return HCCL_SUCCESS;
    }

    rtError_t ret = rtMemcpyD2DAddrAsync(dst, destMax, destOffset, src, count, srcOffset, stream);

    HCCL_DEBUG("Call hrtMemcpyAddrAsync, return value[%d], dstAddr[%p], destMax[%llu], destOffset[%llu], "\
        "srcAddr[%p], count[%llu], srcOffset[%llu]", ret, dst, destMax, destOffset, src, count, srcOffset);

    if (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
        HCCL_WARNING("hrtMemcpyAddrAsync is not supported.", ret);
        return HCCL_E_NOT_SUPPORT;
    }

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[AsyncCopy][Mem]errNo[0x%016llx] rt memory async copy failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], destOffset[%llu], srcAddr[%p], count[%llu], srcOffset[%llu], "\
        "stream[%p].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, destOffset, src, count, srcOffset, stream), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemcpyAddrAsync]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

rtMemcpyKind_t TranslateMemcpyKind(HcclRtMemcpyKind kind)
{
    switch (kind) {
        case HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_HOST:
            return RT_MEMCPY_HOST_TO_HOST;

        case HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE:
            return RT_MEMCPY_HOST_TO_DEVICE;

        case HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_DEVICE_TO_HOST:
            return RT_MEMCPY_DEVICE_TO_HOST;

        case HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_DEVICE_TO_DEVICE:
            return RT_MEMCPY_DEVICE_TO_DEVICE;

        case HcclRtMemcpyKind::HCCL_RT_MEMCPY_ADDR_DEVICE_TO_DEVICE:
            return RT_MEMCPY_ADDR_DEVICE_TO_DEVICE;

        default: {
            HCCL_ERROR("[TranslateMemcpyKind]Not support the memory copy type[%d].", kind);
            return RT_MEMCPY_RESERVED;
        }
    }
}

HcclResult hrtMemAsyncCopy(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    CHK_PTR_NULL(stream);

    CHK_PRT_RET(count == 0, HCCL_WARNING("[hrtMemAsyncCopy] count is zero"), HCCL_SUCCESS);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
    rtError_t ret = rtMemcpyAsync(dst, destMax, src, count, rtKind, stream);
    HCCL_DEBUG("Call hrtMemAsyncCopy, return value[%d], dstAddr[%p], destMax[%llu],"\
        "srcAddr[%p], count[%llu]", ret, dst, destMax, src, count);

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[AsyncCopy][Mem]errNo[0x%016llx] rt memory async copy failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], rtKind[%d], stream[%p].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, rtKind, stream), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemAsyncCopy]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemAsyncCopyWithCfg(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream, uint32_t qosCfg)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    CHK_PTR_NULL(stream);

    CHK_PRT_RET(count == 0, HCCL_WARNING("[hrtMemAsyncCopyWithCfg] count is zero"), HCCL_SUCCESS);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
    rtTaskCfgInfo_t cfgInfo;
    cfgInfo.qos = qosCfg & 0xFU;
    cfgInfo.partId = (qosCfg >> 4U) & 0xFFU;
    rtError_t ret = rtMemcpyAsyncWithCfgV2(dst, destMax, src, count, rtKind, stream, &cfgInfo);
    HCCL_DEBUG("Call rtMemcpyAsyncWithCfgV2, return value[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] "
        "rtKind[%d] stream[%p] qosCfg[%x] cfgInfo.qos[%u] cfgInfo.partId[%u].",
        ret, dst, destMax, src, count, rtKind, stream, qosCfg, cfgInfo.qos, cfgInfo.partId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[AsyncCopy][Mem]errNo[0x%016llx] rt memory async copy failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], rtKind[%d], stream[%p], qosCfg[%x].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, rtKind, stream, qosCfg), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemAsyncCopyWithCfg]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemAsyncCopyByQos(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream, uint32_t qosCfg)
{
#ifndef HCCD
    if (qosCfg == INVALID_QOSCFG) {
        CHK_RET(hrtMemAsyncCopy(dst, destMax, src, count, kind, stream));
    } else {
        CHK_RET(hrtMemAsyncCopyWithCfg(dst, destMax, src, count, kind, stream, qosCfg));
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemAsyncCopyByQos]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemAsyncCopyWithoutCheckKind(void *dst, uint64_t destMax, const void *src, uint64_t count,
    HcclRtMemcpyKind kind, rtStream_t stream)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    CHK_PTR_NULL(stream);

    CHK_PRT_RET(count == 0, HCCL_WARNING("[hrtMemAsyncCopyWithoutCheckKind] count is zero"), HCCL_SUCCESS);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
    rtError_t ret = rtMemcpyAsyncWithoutCheckKind(dst, destMax, src, count, rtKind, stream);

    HCCL_DEBUG("Call rtMemAsyncCopyWithoutCheckKind, return value[%d], dstAddr[%p], destMax[%llu],"\
        "srcAddr[%p], count[%llu]", ret, dst, destMax, src, count);

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[AsyncCopy][Mem]errNo[0x%016llx] rt memory async copy failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], srcAddr[%p], count[%llu], rtKind[%d], stream[%p].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, rtKind, stream), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemAsyncCopyWithoutCheckKind]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetPairDevicesInfo(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue)
{
#ifndef HCCD
    CHK_PTR_NULL(pValue);
    u32 logicIdLocal = 0;
    u32 logicIdDest = 0;
    CHK_RET(hrtGetDeviceIndexByPhyId(phyDevId, logicIdLocal));

    CHK_RET(hrtGetDeviceIndexByPhyId(otherPhyDevId, logicIdDest));

    rtError_t rtRet = rtGetPairDevicesInfo(logicIdLocal, logicIdDest, infoType, reinterpret_cast<int64_t *>(pValue));
    HCCL_DEBUG("rt get pair devices info, return[%d], "
        "para: phyDevId[%u], otherPhyDevId[%u], logicIdLocal[%u], logicIdDest[%u], infoType[%d], value[%p].",
        rtRet, phyDevId, otherPhyDevId, logicIdLocal, logicIdDest, infoType, pValue);
    CHK_PRT_RET(rtRet != RT_ERROR_NONE, HCCL_ERROR("[Get][PairDevicesInfo]errNo[0x%016llx] rt get pair devices "\
        "info failed, return[%d], para: phyDevId[%u], otherPhyDevId[%u], logicIdLocal[%u], logicIdDest[%u], "\
        "infoType[%d], value[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, phyDevId, otherPhyDevId, logicIdLocal,\
        logicIdDest, infoType, pValue), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetPairDevicesInfo]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetPairPhyDevicesInfo(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue,
                                    rtGetPairPhyDevicesInfoPtr funcPtr)
{
#ifndef HCCD
    CHK_PTR_NULL(pValue);
    rtError_t rtRet = funcPtr(phyDevId, otherPhyDevId, infoType, reinterpret_cast<int64_t *>(pValue));
    HCCL_DEBUG("rt get pair devices info, return[%d], para: phyDevId[%u], otherPhyDevId[%u], infoType[%d], value[%p].",
        rtRet, phyDevId, otherPhyDevId, infoType, pValue);
    CHK_PRT_RET(rtRet != RT_ERROR_NONE, HCCL_ERROR("[Get][PairPhyDevicesInfo]errNo[0x%016llx] rt get pair devices "
        "info failed, return[%d], para: phyDevId[%u], otherPhyDevId[%u], infoType[%d], value[%p].",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, phyDevId, otherPhyDevId, infoType, pValue), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetPairPhyDevicesInfo]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetPairDeviceLinkTypeRaw(u32 phyDevId, u32 otherPhyDevId, s32 infoType, s64 *pValue)
{
    static auto funcPtr = (rtGetPairPhyDevicesInfoPtr)g_dlRts.Handle<RT_GET_PAIR_PHY_DEVICES_INFO>();
    if (funcPtr != nullptr) {
        return hrtGetPairPhyDevicesInfo(phyDevId, otherPhyDevId, infoType, pValue, funcPtr);
    }
    return hrtGetPairDevicesInfo(phyDevId, otherPhyDevId, infoType, pValue);
}

HcclResult hrtGetPairDeviceLinkType(u32 phyDevId, u32 otherPhyDevId, LinkTypeInServer &linkType)
{
    if (Is310PDevice()) {
        linkType = LinkTypeInServer::HCCS_TYPE;
        return HCCL_SUCCESS;
    }

    s64 linkTypeRaw = 0;
#ifndef HCCD
    CHK_RET(hrtGetPairDeviceLinkTypeRaw(phyDevId, otherPhyDevId, 0, &linkTypeRaw));
#else
    HCCL_ERROR("[hrtGetPairDeviceLinkTypeRaw]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
    HCCL_INFO("[hrtGetPairDeviceLinkTypeRaw]phyDevId[%u] otherPhyDevId[%u] linkTypeRaw[%d]",
        phyDevId, otherPhyDevId, linkTypeRaw);

    // 若当前为标卡/虚拟机device间通过HCCS直接互联：HCCS_TYPE，device间通过HCCS交换芯片互联：TOPOLOGY_HCCS_SW
    // Ascend910_93* die间为SIO_TYPE
    // 其他情况为PXI_TYPE

    switch (linkTypeRaw) {
        case TOPOLOGY_HCCS:
            linkType = LinkTypeInServer::HCCS_TYPE;
            break;

        case TOPOLOGY_HCCS_SW:
            linkType = LinkTypeInServer::HCCS_SW_TYPE;
            break;

        case TOPOLOGY_SIO:
            linkType = LinkTypeInServer::SIO_TYPE;
            break;

        default:
            linkType = LinkTypeInServer::PXI_TYPE;
    }

    return HCCL_SUCCESS;
}

HcclResult hrtGetPairDevicePhyId(u32 localDevPhyId, u32 &pairDevPhyId)
{
#ifndef HCCD
    DevType deviceType = DevType::DEV_TYPE_COUNT;
    CHK_RET(hrtGetDeviceType(deviceType));
    CHK_PRT_RET(deviceType != DevType::DEV_TYPE_910_93,
        HCCL_ERROR("[hrtGetPairDevicePhyId] only support on 910A3. Please check device type."),
        HCCL_E_NOT_SUPPORT);
 
    // 奇数die对应的phyid - 1，偶数die对应的phyid + 1
    int offset = static_cast<s32>(localDevPhyId) % 2 == 0 ? 1 : -1;

    // 通过本端的phyId获取同一个chip上的另一个die的PhyId
    pairDevPhyId = localDevPhyId + offset;
    LinkTypeInServer linkType = LinkTypeInServer::RESERVED_LINK_TYPE;
    CHK_RET(hrtGetPairDeviceLinkType(localDevPhyId, pairDevPhyId, linkType));
    if (linkType != LinkTypeInServer::SIO_TYPE) {
        pairDevPhyId = localDevPhyId - offset;
        CHK_RET(hrtGetPairDeviceLinkType(localDevPhyId, pairDevPhyId, linkType));
        CHK_PRT_RET(linkType != LinkTypeInServer::SIO_TYPE,
            HCCL_ERROR("[hrtGetPairDevicePhyId] neither of the two neighbour device is a pair device of dev[%u].",
            localDevPhyId), HCCL_E_NOT_SUPPORT);
    }
    HCCL_DEBUG("[hrtGetPairDevicePhyId]GetPairDevicePhyId success, phyDevId[%u], pairDevPhyId[%u]",
        localDevPhyId, pairDevPhyId);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetPairDevicePhyId]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

// 获取指针的属性，主要是页表大小，单位Byte
HcclResult hrtGetPointAttr(HcclRtPointAttr ptrAttr, const void *ptr)
{
    // 参数有效性检查
    CHK_PTR_NULL(ptrAttr);
    CHK_PTR_NULL(ptr);

#ifndef HCCD
    rtError_t ret = rtPointerGetAttributes(reinterpret_cast<rtPointerAttributes_t *>(ptrAttr), ptr);
#else
    static auto funcPtr = (rtError_t(*)(rtPointerAttributes_t *, const void *))g_dlRts.Handle<RT_POINTER_GET_ATTR>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(reinterpret_cast<rtPointerAttributes_t *>(ptrAttr), ptr);
#endif
    HCCL_DEBUG("Call rtPointerGetAttributes, return value[%d], para: ptr[%p].", ret, ptr);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][PointAttr]errNo[0x%016llx] rt get point attr failed, "\
        "return[%d], para: ptrAttrAddr[%p], ptrAddr[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, ptrAttr, ptr),
        HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
}

HcclResult hrtIpcSetMemoryName(const void *ptr, u8 *name, u64 ptrMaxLen, u32 nameMaxLen)
{
    // 参数有效性检查
    CHK_PTR_NULL(ptr);
    CHK_PTR_NULL(name);
#ifndef HCCD
    rtError_t rtRet = rtIpcSetMemoryName(ptr, ptrMaxLen, reinterpret_cast<char *>(name), nameMaxLen);
#else
    static auto funcPtr = (rtError_t(*)(const void*, uint64_t, char*, uint32_t))g_dlRts.Handle<RT_IPC_SET_MEM_NAME>();
    CHK_PTR_NULL(funcPtr);
    rtError_t rtRet = funcPtr(ptr, ptrMaxLen, reinterpret_cast<char *>(name), nameMaxLen);
#endif
    HCCL_INFO("Call rtIpcSetMemoryName, return value[%d], para: ptr[%p], name[%s], byteCount[%llu], nameLen[%u]",
        rtRet, ptr, name, ptrMaxLen, nameMaxLen);
    CHK_PRT_RET(rtRet != RT_ERROR_NONE, HCCL_ERROR("[Set][IpcMemoryName]errNo[0x%016llx] rtSet Ipc Memory Name, "\
        "return[%d], para: ptr[%p] byteCount[%llu].",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), rtRet, ptr, ptrMaxLen), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
}

HcclResult hrtIpcDestroyMemoryName(const u8 *name)
{
    // 参数有效性检查
    CHK_PTR_NULL(name);

#ifndef HCCD
    rtError_t ret = rtIpcDestroyMemoryName(reinterpret_cast<const char *>(name));
#else
    static auto funcPtr = (rtError_t(*)(const char *))g_dlRts.Handle<RT_DESTORY_MEM_NAME>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(reinterpret_cast<const char *>(name));
#endif
    HCCL_INFO("Call rtIpcDestroyMemoryName, return[%d], para: name[%s]", ret, name);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Destroy][IpcMemoryName]errNo[0x%016llx] "\
        "rtDestroy Ipc memory name fail. return[%d], para: name[%s]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, name), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
}

HcclResult hrtIpcOpenMemory(void **ptr, const u8 *name)
{
    CHK_PTR_NULL(ptr);
    CHK_PTR_NULL(name);
#ifndef HCCD
    rtError_t ret = rtIpcOpenMemory(ptr, reinterpret_cast<const char *>(name));
#else
    static auto funcPtr = (rtError_t(*)(void**, const char*))g_dlRts.Handle<RT_IPC_OPEN_MEM>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(ptr, reinterpret_cast<const char *>(name));
#endif
    RPT_CALL_ERR(ret != RT_ERROR_NONE, "rtIpcOpenMemory failed. return[%d], ptr[%p], name[%s]", ret, ptr, name);
    CHK_PRT_RET(ret == ACL_ERROR_RT_MEMORY_ALLOCATION, HCCL_ERROR("[Open][IpcMemory]errNo[0x%016llx] "\
        "rtIpc memory allocation error. return[%d], para: ptr[%p], name[%s]",
        HCCL_ERROR_CODE(HCCL_E_MEMORY), ret, *ptr, name), HCCL_E_MEMORY);

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Open][IpcMemory]errNo[0x%016llx] "\
        "rtOpen ipc memory fail. return[%d], para: ptr[%p], name[%s]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *ptr, name), HCCL_E_RUNTIME);

    HCCL_INFO("Call rtIpcOpenMemory, return value[%d], para: ptr[%p], name[%s].", ret, ptr, name);

    return HCCL_SUCCESS;
}

HcclResult hrtIpcCloseMemory(const void *ptr)
{
    CHK_PTR_NULL(ptr);
#ifndef HCCD
    rtError_t ret = rtIpcCloseMemory(ptr);
#else
    static auto funcPtr = (rtError_t(*)(const void*))g_dlRts.Handle<RT_IPC_CLOSE_MEM>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(ptr);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Close][IpcMemory]errNo[0x%016llx] "\
        "rtClose ipc memory fail, return[%d]. para: ptr[%p]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, ptr), HCCL_E_RUNTIME);
    HCCL_INFO("Call rtIpcCloseMemory, return value[%d], para: ptr[%p]", ret, ptr);

    return HCCL_SUCCESS;
}

HcclResult hrtIpcSetMemoryPid(const u8 *name, int pid[], int num)
{
    CHK_PTR_NULL(name);
    CHK_PTR_NULL(pid);
#ifndef HCCD
    rtError_t ret = rtSetIpcMemPid(reinterpret_cast<const char *>(name), pid, num);
#else
    static auto funcPtr = (rtError_t(*)(const char *, int32_t *, int))g_dlRts.Handle<RT_SET_IPC_MEM_PID>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(reinterpret_cast<const char *>(name), pid, num);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][IpcMemoryPid]errNo[0x%016llx] "\
        "rtSet ipc memory pid fail. return[%d], num[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, num), HCCL_E_RUNTIME);
    HCCL_INFO("Call rtSetIpcMemPid, return value[%d], num[%d], name[%s].", ret, num, name);

    return HCCL_SUCCESS;
}

HcclResult hrtSetIpcMemorySuperPodPid(const u8 *name, s32 peerSdid, s32 peerPid[], s32 pidNum)
{
#ifndef HCCD
    CHK_PTR_NULL(name);
    CHK_PTR_NULL(peerPid);
    rtError_t ret = rtSetIpcMemorySuperPodPid(reinterpret_cast<const char *>(name),
        static_cast<u32>(peerSdid), peerPid, pidNum);
    if (ret != RT_ERROR_NONE) {
        std::string pidStr;
        for (int i = 0; i < pidNum; ++i) {
            pidStr += std::to_string(*(peerPid + i)) + " ";
        }
        HCCL_ERROR("[Set][IpcMemorySuperPodPid]errNo[0x%016llx] "\
            "rtSet ipc memory pid fail. return[%d], name[%s], peerSdid[%016llx], peerPid[%s], pidNum[%d]",
            HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, name, peerSdid, pidStr.c_str(), pidNum);
        return HCCL_E_RUNTIME;
    }
    HCCL_INFO("Call rtSetIpcMemorySuperPodPid, return value[%d], name[%s], peerSdid[%016llx], "\
        "peerPid[%d], pidNum[%d].", ret, name, peerSdid, *peerPid, pidNum);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSetIpcMemorySuperPodPid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtDevMemAlignWithPage(void* &ptr, u64 &size)
{
    // 参数有效性检查
    CHK_PTR_NULL(ptr);

    // 获取页表属性大小
    HcclRtPointAttr ptrAttr = nullptr;
    HcclResult ret = hrtMallocHost(&ptrAttr, sizeof(rtPointerAttributes_t));
    CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[hrtDevMemAlignWithPage]runtime malloc host fail. return[%d]",
        ret), HCCL_E_INTERNAL);

    ret = hrtGetPointAttr(ptrAttr, ptr);
    if (ret != HCCL_SUCCESS) {
        ret = hrtFreeHost(ptrAttr);
        ptrAttr = nullptr;
        CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[hrtDevMemAlignWithPage]runtime free host fail.return[%d]",
            ret), HCCL_E_INTERNAL);
        HCCL_ERROR("[hrtDevMemAlignWithPage]runtime get point attr fail. return[%d]", ret);
        return HCCL_E_INTERNAL;
    }
    int32_t pageSize = (reinterpret_cast<rtPointerAttributes_t *>(ptrAttr))->pageSize;
    CHK_PRT_RET(pageSize < 0,
        {
            ret = hrtFreeHost(ptrAttr);
            ptrAttr = nullptr;
            HCCL_ERROR("[hrtDevMemAlignWithPage]ptr[%p] pageSize[%d] is invalid", ptr, pageSize);
        }, HCCL_E_DRV);
    HCCL_INFO("[hrtDevMemAlignWithPage]get pageSize[%d]", pageSize);
    ret = hrtFreeHost(ptrAttr);
    ptrAttr = nullptr;
    CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[hrtDevMemAlignWithPage]runtime free host fail. return[%d]",
        ret), HCCL_E_INTERNAL);

    // 按照page_size = 0当做没有跨进程映射对齐要求
    if (pageSize == 0) {
        u64 offset = 0;
        size = size + offset;
        return HCCL_SUCCESS;
    }
    // 按页表大小对齐指针
    u64 tmpPtr = reinterpret_cast<u64>(ptr);
    ptr = reinterpret_cast<void *>((reinterpret_cast<u64>(ptr)) & (~(static_cast<u64>(pageSize) - 1)));
    u64 offset = tmpPtr - reinterpret_cast<u64>(ptr);

    // 计算size值
    size = size + offset;

    return HCCL_SUCCESS;
}

HcclResult hrtMemPrefetchToDevice(void *ptr, uint64_t size, int32_t deviceId)
{
#ifndef HCCD
    CHK_PTR_NULL(ptr);

    rtError_t ret = rtMemPrefetchToDevice(ptr, size, deviceId);

    HCCL_DEBUG("Call rtMemPrefetchToDevice, return value[%d], para: ptr[%p] size[%llu Byte] deviceId[%d]", \
               ret, ptr, size, deviceId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Prefetch][MemToDevice]errNo[0x%016llx] "\
        "rtMemPrefetchToDevice, return[%d]. para: ptr[%p] size[%llu Byte] deviceId[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, ptr, size, deviceId), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemPrefetchToDevice]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

#endif

#if T_DESC("host memory管理", true)

HcclResult hrtMallocHost(void **hostPtr, u64 size)
{
    // 参数有效性检查
    CHK_PTR_NULL(hostPtr);
#ifndef HCCD
    rtError_t ret = rtMallocHost(hostPtr, size, HCCL);
#else
    static auto funcPtr = (rtError_t(*)(void**, uint64_t, uint16_t))g_dlRts.Handle<RT_MALLOC_HOST>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(hostPtr, size, HCCL);
#endif
    RPT_ENV_ERR((ret != RT_ERROR_NONE), "EI0007", std::vector<std::string>({"resource_type", "resource_info"}), \
        std::vector<std::string>({"HostMemory", std::string("size:") + std::to_string(size)}));

    HCCL_DEBUG("Call rtMallocHost, return value[%d], para: hostPtr[%p], size[%llu Byte], moudleId: HCCL.",
        ret, *hostPtr, size);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Malloc][Host]errNo[0x%016llx] rt malloc host fail. return[%d], "\
        "para: hostPtr[%p], size[%llu Byte].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *hostPtr, size), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
}

HcclResult hrtFreeHost(void *hostPtr)
{
    // 参数有效性检查
    CHK_PTR_NULL(hostPtr);
#ifndef HCCD
    rtError_t ret = rtFreeHost(hostPtr);
#else
    static auto funcPtr = (rtError_t(*)(void*))g_dlRts.Handle<RT_FREE_HOST>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(hostPtr);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Free][Host]errNo[0x%016llx] rt free host fail. return[%d], "\
        "para: hostPtr[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, hostPtr), HCCL_E_RUNTIME);
    HCCL_DEBUG("Call rtFreeHost, return value[%d].", ret);

    return HCCL_SUCCESS;
}

#endif

#if T_DESC("stream管理", true)

HcclResult hrtStreamActive(HcclRtStream activeStream, HcclRtStream stream)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(activeStream);
    CHK_PTR_NULL(stream);

    rtStream_t rtActiveStream = activeStream;
    rtStream_t rtStream = stream;
    rtError_t ret = rtStreamActive(rtActiveStream, rtStream);
    HCCL_DEBUG("Call rtStreamActive, return value[%d].", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Activate][Stream]errNo[0x%016llx] "\
        "rt stream active fail. return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamActive]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetStreamId(HcclRtStream stream, s32 &streamId)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(stream);

    rtStream_t rtStream = stream;
    rtError_t ret = rtGetStreamId(rtStream, &streamId);

    HCCL_DEBUG("Call rtGetStreamId, return value[%d] streamId[%d].", ret, streamId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][StreamId]errNo[0x%016llx] "\
        "rt get stream ID fail. return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetStreamId]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetTaskIdAndStreamID(u32 &taskId, u32 &streamId)
{
#ifndef HCCD
    rtError_t ret = rtGetTaskIdAndStreamID(&taskId, &streamId);
    HCCL_DEBUG("Call rtGetTaskIdAndStreamId, return value[%d], para: taskId[%u], streamId[%u].", \
        ret, taskId, streamId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][TaskIdAndStreamID]errNo[0x%016llx] "\
        "rt get task ID and stream ID fail. return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetTaskIdAndStreamID]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSwitchStreamEx(void *devMemPtr1, rtCondition_t condition, void *devMemPtr2,
                             HcclRtStream true_stream, HcclRtStream stream, rtSwitchDataType_t dataType)
{
#ifndef HCCD
    CHK_PTR_NULL(devMemPtr1);
    CHK_PTR_NULL(devMemPtr2);
    CHK_PTR_NULL(true_stream);
    CHK_PTR_NULL(stream);

    rtStream_t rtTrueStream = true_stream;
    rtStream_t rtStream = stream;
    rtError_t ret = rtStreamSwitchEx(devMemPtr1, condition, devMemPtr2, rtTrueStream, rtStream, dataType);
    HCCL_DEBUG("Call rtStreamSwitchEx, return value[%d], para: devMemPtr1[%p], condition[%d], devMemPtr2[%p], ",
        ret, devMemPtr1, condition, devMemPtr2);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Switch][StreamEx]errNo[0x%016llx] "\
        "rt stream switch fail. return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSwitchStreamEx]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

#endif

#if T_DESC("event 同步机制", true)
HcclResult hrtEventCreate(HcclRtEvent *event)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    rtError_t ret = rtEventCreate(event);
    RPT_ENV_ERR(ret != RT_ERROR_NONE, "EI0007", std::vector<std::string>({"resource_type","resource_info"}),\
        std::vector<std::string>({"event", "null"}));

    HCCL_DEBUG("Call rtEventCreate, return value[%d], para: event[%p]", ret, *event);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Creat][Event]errNo[0x%016llx] rt event creat, return[%d], "\
        "event[%p]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, event), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtEventCreate]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtEventDestroy(HcclRtEvent event)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    rtError_t ret = rtEventDestroy(event);
    HCCL_DEBUG("Call rtEventDestroy, return value[%d], para: event[%p]", ret, event);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Destroy][Event]errNo[0x%016llx] rt event destroy, return[%d], "\
        "event[%p]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, event), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtEventDestroy]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtEventRecord(HcclRtEvent event, HcclRtStream stream)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    CHK_PTR_NULL(stream);
    rtError_t ret = rtEventRecord(event, stream);
    HCCL_DEBUG("Call rtEventRecord, return value[%d], para: event[%p]", ret, event);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Record][Event]errNo[0x%016llx] rt event record, return[%d], "\
        "event[%p]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, event), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtEventRecord]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamWaitEvent(HcclRtStream stream, HcclRtEvent event)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    CHK_PTR_NULL(stream);
    rtError_t ret = rtStreamWaitEvent(stream, event);
    HCCL_DEBUG("Call rtStreamWaitEvent, return value[%d], para: event[%p]", ret, event);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("errNo[0x%016llx] rt stream wait event, return[%d], event[%p], ",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, event), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamWaitEvent]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtEventQuery(HcclRtEvent event)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    rtError_t ret = rtEventQuery(event);
    return ret == RT_ERROR_NONE ? HCCL_SUCCESS : HCCL_E_RUNTIME;
#else
    HCCL_ERROR("[hrtEventQuery]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
#endif


#if T_DESC("RDMA异步", true)
// 进行单元测试时对改函数打桩可完成条件测试
bool CompareDevType(DevType left, DevType right)
{
    return left == right;
}

HcclResult hrtGetNotifySize(u32 &notifySize)
{
#ifndef HCCD
    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType == DevType::DEV_TYPE_910) {
        notifySize = 8;  // 910A 每个notify占8个字节
    } else if (deviceType == DevType::DEV_TYPE_910B || deviceType == DevType::DEV_TYPE_910_93) {
        notifySize = 4;  // 910B & 910_93 每个notify占4个字节
    } else {
        notifySize = 8;  // 其余芯片类型每个notify占8个字节
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetNotifySize]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyGetOffset(HcclRtNotify notify, u64 &offset)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    auto getEventOffsetFuncPtr = [](HcclRtNotify notify, u64 &offset) -> s32 {
        u32 eventId = 0;
        s32 ret = rtGetEventID(notify, &eventId);
        offset = eventId * 0x8;
        HCCL_INFO("event id[%u] get offset[%llu]", eventId, offset);
        return ret;
    };
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyGetAddrOffset(notify, reinterpret_cast<uint64_t *>(&offset)),
                              getEventOffsetFuncPtr(notify, offset));
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyGetOffset]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyCreate(s32 deviceId, rtNotify_t *notify)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    auto creatEventFuncPtr = [](rtNotify_t *notify) -> s32 {
        CHK_RT_RET(rtEventCreate(notify));
        void *stream = nullptr;
        CHK_RT_RET(rtStreamCreate(&stream, 5));
        CHK_RT_RET(rtEventRecord(*notify, stream));
        CHK_RT_RET(rtEventReset(*notify, stream));
        CHK_RT_RET(rtStreamSynchronize(stream));
        CHK_RT_RET(rtStreamDestroy(stream));
        return 0;
    }; // 使用event替换notify后需要获取event id，get event id 前必须先record，因此在创建event时执行一把record
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyCreate(deviceId, notify), creatEventFuncPtr(notify));
    HCCL_DEBUG("[hrtNotifyCreate] deviceId[%d]", deviceId);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyCreate]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyDestroy(rtNotify_t notify)
{
#ifndef HCCD
    HCCL_DEBUG("[hrtNotifyDestroy]notify[%p]", notify);
    CHK_PTR_NULL(notify);
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyDestroy(notify), rtEventDestroy(notify));
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyDestroy]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyRecord(rtNotify_t notify, rtStream_t stream)
{
#ifndef HCCD
    u32 streamId = 0;
    u32 taskId = 0;
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(stream);
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyRecord(notify, stream), rtEventRecord(notify, stream));
    CHK_RT_RET(rtGetTaskIdAndStreamID(&taskId, &streamId));
    HCCL_INFO("hrtNotifyRecord notify[%p] taskId[%u] streamId[%u]", notify, taskId, streamId);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyRecord]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyWait(rtNotify_t notify, rtStream_t stream)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(stream);

    u32 streamId = 0;
    u32 taskId = 0;
    auto eventWaitFuncPtr = [](rtNotify_t notify, rtStream_t stream) -> s32 {
        CHK_RT_RET(rtStreamWaitEvent(stream, notify));
        CHK_RT_RET(rtEventReset(notify, stream));
        return 0;
    };
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyWait(notify, stream), eventWaitFuncPtr(notify, stream));
    CHK_RT_RET(rtGetTaskIdAndStreamID(&taskId, &streamId));
    HCCL_INFO("hrtNotifyWait notify[%p] taskId[%u] streamId[%u]", notify, taskId, streamId);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyWait]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyWaitWithTimeOut(rtNotify_t notify, rtStream_t stream, uint32_t timeOut)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(stream);

    u32 streamId = 0;
    u32 taskId = 0;
    auto eventWaitFuncPtr = [](rtNotify_t notify, rtStream_t stream) -> s32 {
        CHK_RT_RET(rtStreamWaitEvent(stream, notify));
        CHK_RT_RET(rtEventReset(notify, stream));
        return 0;
    };
    REPLACE_NOTIFY_WITH_EVENT(rtNotifyWaitWithTimeOut(notify, stream, timeOut), eventWaitFuncPtr(stream, notify));
    CHK_RT_RET(rtGetTaskIdAndStreamID(&taskId, &streamId));
    HCCL_INFO("hrtNotifyWaitWithTimeOut notify[%p] taskId[%u] streamId[%u]", notify, taskId, streamId);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyWaitWithTimeOut]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyReset(rtNotify_t notify)
{
#ifndef HCCD
    HCCL_INFO("hrtNotifyReset notify[%p]", notify);
    CHK_PTR_NULL(notify);
    CHK_RT_RET(rtNotifyReset(notify));
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyRecord]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
#endif

#if T_DESC("EnableP2P", true)

HcclResult hrtEnableP2P(u32 deviceLogicId, u32 devicePhyId)
{
#ifndef HCCD
    rtError_t ret = rtEnableP2P(deviceLogicId, devicePhyId, 0);

    HCCL_DEBUG("rt enableP2P device id[%u] and device id[%u] fail[%d]", deviceLogicId, devicePhyId, ret);

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Enable][P2P]errNo[0x%016llx] rt enableP2P device id[%u] and "\
        "device id[%u] fail[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), deviceLogicId, devicePhyId, ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtEnableP2P]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtDisableP2P(u32 deviceLogicId, u32 devicePhyId)
{
#ifndef HCCD
    rtError_t ret = rtDisableP2P(deviceLogicId, devicePhyId);

    HCCL_DEBUG("rt disableP2P device id[%u] and device id[%u] fail[%d]", deviceLogicId, devicePhyId, ret);

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Disable][P2P]errNo[0x%016llx] rt disableP2P device id[%u] and "\
        "device id[%u] fail[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), deviceLogicId, devicePhyId, ret), HCCL_E_RUNTIME);

    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtDisableP2P]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetP2PStatus(u32 deviceLogicId, u32 devicePhyId, u32 *status)
{
#ifndef HCCD
    rtError_t ret = rtGetP2PStatus(deviceLogicId, devicePhyId, status);

    HCCL_DEBUG("rt getp2pstatus device id[%u] and device id[%u] fail[%d]", deviceLogicId, devicePhyId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][P2PStatus]errNo[0x%016llx] rt getp2pstatus device id[%u] "\
        "and device id[%u] fail[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), deviceLogicId, devicePhyId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetP2PStatus]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
#endif

s32 GetMsTimeFromExecTimeout()
{
    s64 timeOutMs;
    timeOutMs = (GetExternalInputHcclExecTimeOut() + HCCL_EXEC_TIME_OUT_OFFSET_S) * TIME_S_TO_MS;
    timeOutMs = (timeOutMs > 0x7FFFFFFF) ? 0x7FFFFFFF : timeOutMs;
    return static_cast<s32>(timeOutMs & (0x7FFFFFFF));
}

HcclResult hcclStreamSynchronize(HcclRtStream stream)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);
    rtError_t ret = rtStreamSynchronizeWithTimeout(stream, GetMsTimeFromExecTimeout());
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Synchronize][Stream]errNo[0x%016llx] rt "\
        "streamsynchronizewithtimeout fail. return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hcclStreamSynchronize]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtTaskAbortHandleCallback(rtTaskAbortCallBack callback, void *args)
{
#ifndef HCCD
    rtError_t ret = rtSetTaskAbortCallBack("HCCL", callback, args);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Reg][TaskAbortCallBack]errNo[0x%016llx] rt reg taskabortcallback "\
        "fail. return[%d], para: callback[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, callback), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtTaskAbortHandleCallback]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult PrintMemoryAttr(const void *memAddr)
{
#ifndef HCCD
    if (LIKELY(!HcclCheckLogLevel(HCCL_LOG_INFO))) {
        return HCCL_SUCCESS;
    }

    HcclResult ret;
    rtPointerAttributes_t memAttr;
    CHK_PTR_NULL(memAddr);
    s32 sRet = memset_s(&memAttr, sizeof(rtPointerAttributes_t), 0, sizeof(rtPointerAttributes_t));
    CHK_PRT_RET(sRet != EOK, HCCL_ERROR("[Print][MemoryAttr]errNo[0x%016llx]memory set 0 failed for memAttr. "\
        "params: dest[%p], destMaxSize[%zu], count[%zu]", HCOM_ERROR_CODE(HCCL_E_MEMORY), &memAttr,
        sizeof(rtPointerAttributes_t), sizeof(rtPointerAttributes_t)), HCCL_E_MEMORY);
    ret = hrtGetPointAttr(&memAttr, memAddr);
    CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[Print][MemoryAttr]errNo[0x%016llx] runtime get memory attr failed",
        HCOM_ERROR_CODE(ret)), ret);
    HCCL_INFO("memory attributes: address[%p], page size[%u], type[%d]", memAddr, memAttr.pageSize, memAttr.memoryType);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[PrintMemoryAttr]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
HcclResult hrtRegTaskFailCallbackByModule(rtTaskFailCallback callback)
{
#ifndef HCCD
    rtError_t ret = rtRegTaskFailCallbackByModule("HCCL", callback);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Reg][TaskFailCallback]errNo[0x%016llx] rt reg taskFailCallback "\
        "fail. return[%d], para: callback[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, callback), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtRegTaskFailCallbackByModule]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
HcclResult hrtGetMaxStreamAndTask(u32 &maxStrCount, u32 &maxTaskCount)
{
#ifndef HCCD
    rtError_t ret = rtGetMaxStreamAndTask(RT_NORMAL_STREAM, &maxStrCount, &maxTaskCount);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][MaxStreamAndTask]errNo[0x%016llx] rt GetMaxStreamAndTask "\
        "fail. return[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetMaxStreamAndTask]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
HcclResult hrtSubscribeReport(u64 threadId, rtStream_t &stream)
{
#ifndef HCCD
    rtError_t ret = rtSubscribeReport(threadId, stream);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[HostNic][RegStream]errNo[0x%016llx] rtSubscribeReport fail,"
        "return[%d], para: threadId[%llu].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, threadId), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSubscribeReport]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
HcclResult hrtUnSubscribeReport(u64 threadId, rtStream_t &stream)
{
#ifndef HCCD
    rtError_t ret = rtUnSubscribeReport(threadId, stream);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Close][HostNicThread]errNo[0x%016llx] rtUnSubscribeReport fail,"
        "return[%d], para: threadId[%llu].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, threadId), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtUnSubscribeReport]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}
HcclResult hrtProcessReport(s32 timeout)
{
#ifndef HCCD
    rtError_t ret = rtProcessReport(timeout);
    if (ret != RT_ERROR_NONE) {
        HCCL_INFO("rtProcessReport timeout, return[%d]", ret);
        return HCCL_E_TIMEOUT;
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtProcessReport]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtDeviceGetBareTgid(u32 *pid)
{
    CHK_PTR_NULL(pid);
#ifndef HCCD
    rtError_t ret = rtDeviceGetBareTgid(pid);
    HCCL_DEBUG("Call rtDeviceGetBareTgid, return value[%d], rtGet pid[%u].", ret, *pid);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Get][BareTgid]errNo[0x%016llx] rtGet pid fail, "
        "return[%d], rtGet pid[%u]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *pid), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    if (g_workModeAicpu) {
        // aicpu不使用PID。
        *pid = 0;
        return HCCL_SUCCESS;
    }
    HCCL_ERROR("[hrtDeviceGetBareTgid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtIpcOpenNotify(rtNotify_t* notify, const u8 *name)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(name);

    rtError_t ret = rtIpcOpenNotify(notify, reinterpret_cast<const char *>(name));
    HCCL_INFO("Call rtIpcOpenNotify, return value[%d] para: notify[%p], name[%s].", ret, *notify, name);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][IpcOpenNotify]errNo[0x%016llx] rt ipc notify open fail,"\
        "return[%d]. para: notify[%p], name[%s]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *notify, name),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtIpcOpenNotify]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSetIpcNotifyPid(const u8 *name, int32_t pid[], int num)
{
#ifndef HCCD
    CHK_PTR_NULL(name);
    CHK_PTR_NULL(pid);

    rtError_t ret = rtSetIpcNotifyPid(reinterpret_cast<const char *>(name),  pid, num);
    HCCL_DEBUG("Call rtSetIpcNotifyPid, return value[%d]. Params: num[%d].", ret, num);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][IpcNotifyPid]errNo[0x%016llx] "
        "rtSet ipc Notify pid fail. return[%d], num[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, num), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSetIpcNotifyPid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSetIpcNotifySuperPodPid(const u8 *name, s32 peerSdid, s32 peerPid[], s32 pidNum)
{
#ifndef HCCD
    CHK_PTR_NULL(name);
    CHK_PTR_NULL(peerPid);

    for (int i = 0; i < pidNum; ++i) {
        rtError_t ret = rtSetIpcNotifySuperPodPid(reinterpret_cast<const char *>(name),
            static_cast<u32>(peerSdid), peerPid[i]);
        CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][IpcNotifyPid]errNo[0x%016llx] "
            "rtSet ipc Notify pid fail. return[%d], name[%s], peerSdid[%016llx], peerPid[%d]=%d",
            HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, name, peerSdid, i, peerPid[i]), HCCL_E_RUNTIME);
        HCCL_DEBUG("Call rtSetIpcNotifySuperPodPid, return value[%d]. "\
            "Params: name[%s], peerSdid[%016llx], peerPid[%d], num[%d].", ret, name, peerSdid, peerPid[i], pidNum);
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSetIpcNotifySuperPodPid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtIpcIntNotice(const rtIpcIntNoticeInfo_t * const Info, rtStream_t stm)
{
#ifndef HCCD
    CHK_PTR_NULL(Info);
    CHK_PTR_NULL(stm);
    rtError_t ret = rtIpcIntNotice(Info, stm);
    HCCL_DEBUG("Call rtIpcIntNotice, return value[%d].para: NoticeInfo[%p].", ret, *Info);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][rtIpcIntNotice]errNo[0x%016llx] rt IpcInt Notice open fail,"\
        "return[%d]. para: NoticeInfo[%p]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *Info),
        HCCL_E_RUNTIME);
    HCCL_DEBUG("[hrtIpcIntNotice] ntcPid[%u] ntcGrpId[%u] ntcTid[%u] ntcSubEventId[%u] ntcEventId[%u] msgLen[%u]\
        msg[%s]", Info->ntcPid, Info->ntcGrpId, Info->ntcTid, Info->ntcSubEventId, Info->ntcEventId,\
        Info->msgLen, Info->msg);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtIpcIntNotice]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtCtxGetOverflowAddr(void **overflowAddr)
{
#ifndef HCCD
    CHK_PTR_NULL(overflowAddr);

    rtError_t ret = rtCtxGetOverflowAddr(overflowAddr);
    HCCL_DEBUG("Call rtCtxGetOverflowAddr, return value[%d].", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][rtCtxGetOverflowAddr]errNo[0x%016llx] "
        "rtCtx get overflow addr fail. return[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtCtxGetOverflowAddr]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtReduceAsyncV2(void* dst, uint64_t destMax, const void* src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream, void* overflowAddr)
{
#ifndef HCCD
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);

    rtError_t ret = rtReduceAsyncV2(dst, destMax, src, count, kind, type, stream, overflowAddr);
    HCCL_DEBUG("Call rtReduceAsyncV2, return value[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] rtReduceOp[%d] "
        "rtDataType[%d].", ret, dst, destMax, src, count, kind, type);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][ReduceAsync]errNo[0x%016llx] rt reduce async fail,"\
        "return[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] rtReduceOp[%d] rtDataType[%d] "\
        "overflowAddr[%p].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind, type,
        overflowAddr), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtReduceAsyncV2]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtReduceAsync(void* dst, uint64_t destMax, const void* src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream)
{
#ifndef HCCD
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);

    rtError_t ret = rtReduceAsync(dst, destMax, src, count, kind, type, stream);
    HCCL_DEBUG("Call rtReduceAsync, return value[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] rtReduceOp[%d] "
        "rtDataType[%d].", ret, dst, destMax, src, count, kind, type);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][ReduceAsync]errNo[0x%016llx] rt reduce async fail,"\
        "return[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] rtReduceOp[%d] rtDataType[%d].", \
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind, type), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtReduceAsync]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtReduceAsyncWithCfg(void *dst, uint64_t destMax, const void *src, uint64_t count, rtRecudeKind_t kind,
    rtDataType_t type, rtStream_t stream, uint32_t qosCfg)
{
#ifndef HCCD
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    rtTaskCfgInfo_t cfgInfo;
    cfgInfo.qos = qosCfg & 0xFU;
    cfgInfo.partId = (qosCfg >> 4U) & 0xFFU;
    rtError_t ret = rtReduceAsyncWithCfgV2(dst, destMax, src, count, kind, type, stream, &cfgInfo);
    HCCL_DEBUG("Call rtReduceAsyncWithCfgV2, return value[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] "
        "rtReduceOp[%d] rtDataType[%d] stream[%p] qosCfg[%x] cfgInfo.qos[%u] cfgInfo.partId[%u].",
        ret, dst, destMax, src, count, kind, type, stream, qosCfg, cfgInfo.qos, cfgInfo.partId);
    CHK_PRT_RET(ret != RT_ERROR_NONE,
        HCCL_ERROR("[rt][ReduceAsync]errNo[0x%016llx] rt reduce async fail,"
        "return[%d]. para: dst[%p] destMax[%llu] src[%p] count[%llu] rtReduceOp[%d] rtDataType[%d] stream[%p] "
        "qosCfg[%x].",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, src, count, kind, type, stream, qosCfg),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtReduceAsync]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtCallbackLaunch(rtCallback_t callBackFunc, void *fnData, rtStream_t stream, bool isBlock)
{
#ifndef HCCD
    CHK_PTR_NULL(fnData);

    rtError_t ret = rtCallbackLaunch(callBackFunc, fnData, stream, isBlock);
    HCCL_DEBUG("Call rtCallbackLaunch, return value[%d].", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][CallbackLaunch]callback launch failed, ret[%d]", ret),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtCallbackLaunch]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtRDMASend(u32 qpn, u32 wqe_index, rtStream_t stream)
{
#ifndef HCCD
    rtError_t ret = rtRDMASend(qpn, wqe_index, stream);
    HCCL_DEBUG("Call rtRDMASend, return value[%d]. Params: qpn[%u] wqe_index[%u].", ret, qpn, wqe_index);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rt][RdmaSend]errNo[0x%016llx] rt rdma send fail, "\
        "return[%d]. para: qpn[%u] wqe_index[%u].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, qpn, wqe_index),\
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtRDMASend]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtIpcSetNotifyName(rtNotify_t notify, u8* name, uint32_t len)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(name);

    rtError_t ret = rtIpcSetNotifyName(notify, reinterpret_cast<char *>(name), len);
    HCCL_INFO("[hrtIpcSetNotifyName] Call rtIpcSetNotifyName, name[%s] return value[%d].", name, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Set][IPCNotify]errNo[0x%016llx] IPC set notify name[%s] fail. "\
        "return[%d].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), name, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtIpcSetNotifyName]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamDestroy(rtStream_t stream)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);
    rtError_t ret = rtStreamDestroyForce(stream);
    HCCL_DEBUG("Call rtStreamDestroyForce, return value[%d].", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Stream][Destroy]errNo[0x%016llx] rt stream Destroy fail, "
        "return[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamDestroy]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamCreate(rtStream_t *stream, int32_t priority)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);

    rtError_t ret = rtStreamCreate(stream, priority);
    HCCL_DEBUG("Call rtStreamCreate, return value[%d].", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Stream][Create]errNo[0x%016llx] rtStreamCreate error, "
        "rtRet[%d]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamCreate]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamCreateWithFlags(rtStream_t * stream, int32_t priority, uint32_t flags)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);

    rtError_t ret = rtStreamCreateWithFlags(stream, priority, flags);
    HCCL_DEBUG("Call rtGetStreamId return value[%d]. Params: flags[%u].", ret, flags);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Stream][CreateWithFlags]errNo[0x%016llx] rtStreamCreate error, "
        "rtRet[%d], flags[%u]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, flags), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamCreateWithFlags]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamSetMode(rtStream_t stream, const uint64_t stmMode)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);
    s32 streamId = -1;
    rtError_t ret = rtGetStreamId(stream, &streamId);
    HCCL_DEBUG("Call rtGetStreamId, return value[%d].", ret);

    ret = rtStreamSetMode(stream, stmMode);
    HCCL_DEBUG("Call rtStreamSetMode return value[%d]. stmMode[%llu], streamId[%d].", ret, stmMode, streamId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_WARNING("[Stream][SetMode]errNo[0x%016llx] rtStreamSetMode fail, "
        "nothing changed. rtRet[%d], stmMode[%llu]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, stmMode), HCCL_SUCCESS);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamSetMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamGetMode(rtStream_t const stream, uint64_t *const stmMode)
{
#ifndef HCCD
    CHK_PTR_NULL(stream);

    s32 streamId = -1;
    rtError_t ret = rtGetStreamId(stream, &streamId);
    HCCL_DEBUG("Call rtGetStreamId, return value[%d].", ret);

    ret = rtStreamGetMode(stream, stmMode);
    HCCL_DEBUG("Call rtStreamGetMode return value[%d]. stmMode[%llu], streamId[%d].", ret, *stmMode, streamId);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[Stream][GetMode]errNo[0x%016llx] rtStreamGetMode error, "
        "rtRet[%d], stmMode[%llu]", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, *stmMode), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtStreamGetMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtDevBinaryRegister(const rtDevBinary_t *bin, BinHandle *handle)
{
#ifndef HCCD
    rtError_t ret = rtDevBinaryRegister(bin, handle);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtDev][inaryRegister]register opBinary failed"), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtDevBinaryRegister]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemcpy(void *dst, uint64_t destMax, const void *src, uint64_t count, HcclRtMemcpyKind kind)
{
    CHK_PTR_NULL(dst);
    CHK_PTR_NULL(src);
    rtMemcpyKind_t rtKind = TranslateMemcpyKind(kind);
#ifndef HCCD
    rtStreamCaptureMode mode = rtStreamCaptureMode::RT_STREAM_CAPTURE_MODE_RELAXED;
    HcclResult hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemcpy] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
    rtError_t ret = rtMemcpy(dst, destMax, src, count, rtKind);
    hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemcpy] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
#else
    rtError_t ret = RT_ERROR_NONE;
    static auto funcPtr =
        (rtError_t(*)(void *, uint64_t, const void *, uint64_t, rtMemcpyKind_t))g_dlRts.Handle<RT_MEMCPY>();
    CHK_PTR_NULL(funcPtr);
    ret = funcPtr(dst, destMax, src, count, rtKind);
#endif
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtMemcpy] rt data memcpy host to device failed,"
        "size[%llu Byte] src[%p]", destMax, src), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
}

HcclResult hrtKernelLaunch(const void *stubFunc, uint32_t blockDim, void *args, uint32_t argsSize,
                            rtSmDesc_t *smDesc, rtStream_t stream)
{
#ifndef HCCD
    HCCL_DEBUG("[hrtKernelLaunch]blockdim: [%u]", blockDim);
    rtError_t ret = rtKernelLaunch(stubFunc, blockDim, args,  argsSize, smDesc, stream);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtKernelLaunch]execute kernel launch failed"), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtKernelLaunch]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtKernelLaunchWithFlagV2(const void *stubFunc, uint32_t blockDim, rtArgsEx_t *argsInfo, rtSmDesc_t *smDesc,
    rtStream_t stream, uint32_t flags, const rtTaskCfgInfo_t *cfgInfo)
{
#ifndef HCCD
    HCCL_DEBUG("[hrtKernelLaunchWithFlagV2]blockdim: [%u]", blockDim);
    rtError_t ret = rtKernelLaunchWithFlagV2(stubFunc, blockDim, argsInfo, smDesc, stream, flags, cfgInfo);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtKernelLaunchWithFlagV2]execute kernel launch failed"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtKernelLaunchWithFlagV2]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult  hrtFunctionRegister(BinHandle binHandle, const void *stubFunc, const char *stubName, const void *devFunc,
                                uint32_t funcMode)
{
#ifndef HCCD
    rtError_t ret = rtFunctionRegister(binHandle, stubFunc, stubName, devFunc, funcMode);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtFunctionRegister]function register failed"), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtFunctionRegister]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtRDMADBSend(uint32_t dbindex, uint64_t dbinfo, rtStream_t stream)
{
#ifndef HCCD
    // stream为空时使用rts内部set device后的默认stream
    rtError_t ret = rtRDMADBSend(dbindex, dbinfo, stream);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtRDMADBSend]errNo[0x%016llx] rt rdma send fail, "
        "return[%d]. para: dbindex[%u]dbinfo[%llu].", HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dbindex,
        dbinfo), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    static auto funcPtr = (rtError_t(*)(uint32_t, uint64_t, rtStream_t))g_dlRts.Handle<RT_RDMA_DB_SEND>();
    CHK_PTR_NULL(funcPtr);
    rtError_t ret = funcPtr(dbindex, dbinfo, stream);
    HCCL_DEBUG("Call rtRDMADBSend, return value[%d]", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("rtRDMADBSend errNo[0x%016llx] RDMADBSend fail, return[%d]",
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#endif
}

HcclResult hrtDevBinaryUnRegister(BinHandle handle)
{
#ifndef HCCD
    CHK_PTR_NULL(handle);
    rtError_t ret = rtDevBinaryUnRegister(handle);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtDevBinaryUnRegister]rt dev binary unregister failed"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtDevBinaryUnRegister]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSetLocalDeviceSatMode(rtFloatOverflowMode_t floatOverflowMode)
{
#ifdef HCCD
    g_deviceSatMode = floatOverflowMode;
#endif
    return HCCL_SUCCESS;
}

HcclResult hrtGetDeviceSatMode(rtFloatOverflowMode_t * floatOverflowMode)
{
#ifndef HCCD
    CHK_PTR_NULL(floatOverflowMode);
    rtError_t ret = rtGetDeviceSatMode(floatOverflowMode);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtGetDeviceSatMode]rt get dev satmode failed,"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    if (g_workModeAicpu) {
        *floatOverflowMode = g_deviceSatMode;
        return HCCL_SUCCESS;
    }
    HCCL_ERROR("[hrtGetDeviceSatMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtSetDeviceSatMode(rtFloatOverflowMode_t floatOverflowMode)
{
#ifndef HCCD
    rtError_t ret = rtSetDeviceSatMode(floatOverflowMode);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtSetDeviceSatMode]rt set dev satmode failed,"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtSetDeviceSatMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtAicpuKernelLaunch(const rtKernelLaunchNames_t &launchNames,
    uint32_t blockDim, const void *args, uint32_t argsSize, rtSmDesc_t *smDesc, rtStream_t stm)
{
#ifndef HCCD
    rtArgsEx_t argsInfo = {0};
    argsInfo.args = const_cast<void *>(args);
    argsInfo.argsSize = argsSize;
    argsInfo.isNoNeedH2DCopy = false;
    rtError_t ret = rtAicpuKernelLaunchWithFlag(&launchNames, blockDim, &argsInfo, smDesc, stm, H2D_COPY_FLAG);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtAicpuKernelLaunch]rt aicpu kernel launch failed,"),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtAicpuKernelLaunch]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtFftsPlusTaskLaunchWithFlag(rtFftsPlusTaskInfo_t *fftsPlusTaskInfo, rtStream_t stm, u32 flag)
{
#ifndef HCCD
    CHK_PTR_NULL(fftsPlusTaskInfo);
    CHK_PTR_NULL(stm);
    uintptr_t input[2];     // fftsplus task下发时需要输入2个参数. 0: task info, 1: stream handle
    input[0] = reinterpret_cast<uintptr_t>(fftsPlusTaskInfo);
    input[1] = reinterpret_cast<uintptr_t>((stm));

    HcclUs startut = TIME_NOW();
    rtError_t  ret = rtGeneralCtrl(input, 2, RT_GNL_CTRL_TYPE_FFTS_PLUS); // 2: fftsplus task有2个input

    HcclUs endut = TIME_NOW();
    HCCL_INFO("rtFftsPlusTaskLaunchWithFlag: take time [%lld]us", DURATION_US(endut - startut));

    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtFftsPlusTaskLaunchWithFlag]rt ffts launch failed."),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtFftsPlusTaskLaunchWithFlag]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyGetAddr(HcclRtNotify signal, u64 *notifyAddr)
{
#ifndef HCCD
    uint64_t* const addr = reinterpret_cast<uint64_t*>(notifyAddr);
    rtError_t ret = rtGetNotifyAddress(signal, addr);
    HCCL_DEBUG("Call rtGetNotifyAddress, signal[%p], notifyAddr[%016llx]", signal, *notifyAddr);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtGetNotifyAddress]rt get notify address failed."), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtNotifyGetAddr]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetNotifyID(HcclRtNotify signal, u32 *notifyID)
{
#ifndef HCCD
    CHK_PTR_NULL(signal);
    CHK_PTR_NULL(notifyID);
    rtError_t ret = rtGetNotifyID(signal, notifyID);
    HCCL_DEBUG("Call rtGetNotifyID signal:%p, notifyID:%u.", signal, *notifyID);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtGetNotifyID]rt get notify id failed."), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetNotifyID]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetDeviceInfo(u32 deviceId, HcclRtDeviceModuleType hcclModuleType,
    HcclRtDeviceInfoType hcclInfoType, s64 &val)
{
#ifndef HCCD
    rtDeviceModuleType_t moduleType;
    switch (hcclModuleType) {
        case HcclRtDeviceModuleType::HCCL_RT_MODULE_TYPE_SYSTEM:
            moduleType = RT_MODULE_TYPE_SYSTEM;
            break;

        case HcclRtDeviceModuleType::HCCL_RT_MODULE_TYPE_AICORE:
            moduleType = RT_MODULE_TYPE_AICORE;
            break;

        case HcclRtDeviceModuleType::HCCL_RT_MODULE_TYPE_VECTOR_CORE:
            moduleType = RT_MODULE_TYPE_VECTOR_CORE;
            break;

        default: {
            HCCL_ERROR("[hrtGetDeviceInfo]Not support the rt module type[%d].", hcclModuleType);
            return HCCL_E_NOT_SUPPORT;
        }
    }

    s32 infoType = 0;
    switch (hcclInfoType) {
        case HcclRtDeviceInfoType::HCCL_INFO_TYPE_CORE_NUM:
            infoType = INFO_TYPE_CORE_NUM;
            break;

        case HcclRtDeviceInfoType::HCCL_INFO_TYPE_PHY_CHIP_ID:
            infoType = INFO_TYPE_PHY_CHIP_ID;
            break;

        case HcclRtDeviceInfoType::HCCL_INFO_TYPE_SDID:
            infoType = INFO_TYPE_SDID;
            break;

        case HcclRtDeviceInfoType::HCCL_INFO_TYPE_SERVER_ID:
            infoType = INFO_TYPE_SERVER_ID;
            break;

        case HcclRtDeviceInfoType::HCCL_INFO_TYPE_SUPER_POD_ID:
            infoType = INFO_TYPE_SUPER_POD_ID;
            break;

        default: {
            HCCL_ERROR("[hrtGetDeviceInfo]Not support the rt info type[%d].", hcclInfoType);
            return HCCL_E_NOT_SUPPORT;
        }
    }
    rtError_t ret = rtGetDeviceInfo(deviceId, moduleType, infoType, reinterpret_cast<int64_t *>(&val));
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtGetDeviceInfo]rt get device info failed."), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetDeviceInfo]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetRdmaDoorbellAddr(u32 dbIndex, u64 &dbAddr)
{
#ifndef HCCD
    s32 devLogID = 0;
    s64 chipID = 0;
    static std::mutex devChipIdMapSpinMutex; // devLogID 和 chipID 关系表的读写互斥锁

    CHK_RET(hrtGetDevice(&devLogID));

    // 读写表前先加锁
    std::unique_lock<std::mutex> lockDevChipIdMap(devChipIdMapSpinMutex);

    if (g_deviceChipIdMap.find(devLogID) != g_deviceChipIdMap.end()) {
        // 若已有记录，则直接获取chipID
        chipID = g_deviceChipIdMap[devLogID];
    } else {
        CHK_RET(hrtGetDeviceInfo(devLogID, HcclRtDeviceModuleType::HCCL_RT_MODULE_TYPE_SYSTEM,
            HcclRtDeviceInfoType::HCCL_INFO_TYPE_PHY_CHIP_ID, chipID));
        g_deviceChipIdMap[devLogID] = chipID;
    }
    // 解锁
    lockDevChipIdMap.unlock();

    u64 roceBaseAddr;
    u64 roceVfDbCfg0Reg;
    u64 chipAddrOffset;
    u64 dieAddrOffset;
    u32 dbDieIdMask;
    u32 dbDieIdShift;

    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType == DevType::DEV_TYPE_910_93) {
        HCCL_DEBUG("[hrtGetRdmaDoorbellAddr] The roceBaseAddr and dieAddrOffset has changed, when deviceType is 910_93.");
        // 910_93 HCCS_SW 组网
        roceBaseAddr = 0x202000000000ULL;
        roceVfDbCfg0Reg = 0x230ULL;
        chipAddrOffset = 0x20000000000ULL;
        dieAddrOffset = 0x10000000000ULL;
        dbDieIdMask = 0x00ff0000;
        dbDieIdShift = 16; // 16 is dbDieIdShift
    } else {
        roceBaseAddr = 0x2000000000ULL;
        roceVfDbCfg0Reg = 0x230ULL;
        chipAddrOffset = 0x80000000000ULL;
        dieAddrOffset = 0x10000000000ULL;
        dbDieIdMask = 0x00ff0000;
        dbDieIdShift = 16; // 16 is dbDieIdShift
    }

    dbAddr = roceBaseAddr + roceVfDbCfg0Reg + chipAddrOffset * chipID +
        dieAddrOffset * ((dbIndex & dbDieIdMask) >> dbDieIdShift);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetRdmaDoorbellAddr]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtKernelGetAddrAndPrefCnt(void *hdl, const uint64_t tilingKey, const void * const stubFunc,
    const uint32_t flag, void **addr, uint32_t *prefetchCnt)
{
#ifndef HCCD
    rtError_t ret = rtKernelGetAddrAndPrefCnt(hdl, tilingKey, stubFunc, flag, addr, prefetchCnt);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtKernelGetAddrAndPrefCnt]rtKernel Get Addr failed."),
        HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtKernelGetAddrAndPrefCnt]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetDevArgsAddr(rtStream_t stm, rtArgsEx_t *argsInfo, void **devArgsAddr, void **argsHandle)
{
#ifndef HCCD
    rtError_t ret = rtGetDevArgsAddr(stm, argsInfo, devArgsAddr, argsHandle);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtGetDevArgsAddr]rtKernel Get Addr failed."), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetDevArgsAddr]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtMemSet(void *dst, uint64_t destMax, uint64_t count)
{
#ifndef HCCD
    // 参数有效性检查
    CHK_PTR_NULL(dst);
    rtStreamCaptureMode mode = rtStreamCaptureMode::RT_STREAM_CAPTURE_MODE_RELAXED;
    HcclResult hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemSet] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
    rtError_t ret = rtMemset(dst, destMax, 0, count);

    HCCL_DEBUG("Call rtMemset, return value[%d]", ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[AsyncCopy][Mem]errNo[0x%016llx] rt memory async set failed, "\
        "return[%d], para: dstAddr[%p], destMax[%llu], count[%llu].",\
        HCCL_ERROR_CODE(HCCL_E_RUNTIME), ret, dst, destMax, count), HCCL_E_RUNTIME);
    hcclRet = hrtThreadExchangeCaptureMode(&mode);
    CHK_PRT_CONT(hcclRet != HCCL_SUCCESS && hcclRet != HCCL_E_NOT_SUPPORT,
        HCCL_ERROR("[hrtMemSet] hrtThreadExchangeCaptureMode return [%d]", hcclRet));
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtMemSet]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtEventCreateWithFlag(rtEvent_t *evt)
{
#ifndef HCCD
    CHK_PTR_NULL(evt);

    rtError_t ret = rtEventCreateWithFlag(evt, RT_EVENT_MC2);
    HCCL_DEBUG("Call rtEventCreateWithFlag, return value[%d] evt[%p].", ret, evt);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtEventCreateWithFlag]rtEvent Create WithFlags failed "\
        "value[%d] evt[%p].", ret, evt), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtEventCreateWithFlag]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyCreateWithFlag(int32_t deviceId, rtNotify_t *notify)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);

    rtError_t ret = rtNotifyCreateWithFlag(deviceId, notify, RT_NOTIFY_MC2);
    HCCL_DEBUG("Call rtNotifyCreateWithFlag deviceId:[%d], return value[%d].", deviceId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtNotifyCreateWithFlag]rtNotify Create WithFlags failed "\
        "deviceId:%d, notify:%p, return value[%d].", deviceId, notify, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtNotifyCreateWithFlag]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtIpcOpenNotifyWithFlag(rtNotify_t *notify, const u8 *name, uint32_t flags)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(name);

    rtError_t ret = rtIpcOpenNotifyWithFlag(notify,
        reinterpret_cast<const char *>(name), flags);
    HCCL_DEBUG("Call rtIpcOpenNotifyWithFlag notify:[%p], name:[%s], flags[%u], return value[%d].", notify, name,
        flags, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtIpcOpenNotifyWithFlag]rtIpc OpenNotify WithFlags failed "\
        "notify:%p, name:%s, return value[%d].", notify, name, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtIpcOpenNotifyWithFlag]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyGetPhyInfo(rtNotify_t notify, uint32_t *phyDevId, uint32_t *tsId)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(phyDevId);
    CHK_PTR_NULL(tsId);

    rtError_t ret = rtNotifyGetPhyInfo(notify, phyDevId, tsId);
    HCCL_DEBUG("Call rtNotifyGetPhyInfo notify:%p, phyDevId:%u, tsId:%u, return value[%d].", notify, *phyDevId,
        *tsId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtNotifyGetPhyInfo]rtNotify GetPhy failed notify:%p, phyDevId:%u, "\
        "tsId:%u, return value[%d].", notify, *phyDevId, *tsId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtNotifyGetPhyInfo]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtNotifyGetPhyInfoExt(rtNotify_t notify, rtNotifyPhyInfo *notifyInfo)
{
#ifndef HCCD
    CHK_PTR_NULL(notify);
    CHK_PTR_NULL(notifyInfo);

    rtError_t ret = rtNotifyGetPhyInfoExt(notify, notifyInfo);
    HCCL_DEBUG("Call rtNotifyGetPhyInfoExt notify:%p, phyId:%u, tsId:%u, idType:%u, notifyid:[%u], flag:[%u], "
        "return value[%d].", notify, notifyInfo->phyId, notifyInfo->tsId, notifyInfo->idType, notifyInfo->shrId,
        notifyInfo->flag, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("Call rtNotifyGetPhyInfoExt notify:%p, phyId:%u, tsId:%u, "
        "idType:%u, notifyid:[%u], flag:[%u], return value[%d].", notify, notifyInfo->phyId, notifyInfo->tsId,
        notifyInfo->idType, notifyInfo->shrId, notifyInfo->flag, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtNotifyGetPhyInfoExt]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetEventID(rtEvent_t event, uint32_t *eventId)
{
#ifndef HCCD
    CHK_PTR_NULL(event);
    CHK_PTR_NULL(eventId);

    rtError_t ret = rtGetEventID(event, eventId);
    HCCL_DEBUG("Call rtGetEventID event:%p, eventId:%u, return value[%d].", event, *eventId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtGetEventID]rtGet EventID failed event:%p, eventId:%u"\
        "return value[%d].", event, *eventId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetEventID]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtAicpuKernelLaunchExWithArgs(uint32_t kernelType, const char *opName, uint32_t blockDim,
    const rtAicpuArgsEx_t *argsInfo, rtSmDesc_t *smDesc,
    rtStream_t stream, uint32_t flags)
{
#ifndef HCCD
    CHK_PTR_NULL(opName);
    CHK_PTR_NULL(argsInfo);

    rtError_t ret = rtAicpuKernelLaunchExWithArgs(kernelType, opName, blockDim,
        argsInfo, smDesc, stream, flags);
    HCCL_DEBUG("Call rtAicpuKernelLaunchExWithArgs kernelType:%u opName:%s blockDim %u "
        "argsInfo %p smDesc %p stream %p flags %u return value[%d].", kernelType, opName, blockDim, argsInfo, smDesc,
        stream, flags, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtAicpuKernelLaunchExWithArgs]rtAicpu KernelLaunch ExWithArgs "\
        "failed kernelType:%u opName:%s blockDim %u argsInfo %p smDesc %p stream %p flags %u return value[%d].",
        kernelType, opName, blockDim, argsInfo, smDesc, stream, flags, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetEventID]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamGetSqid(const rtStream_t stm, uint32_t *sqId)
{
#ifndef HCCD
    CHK_PTR_NULL(stm);
    CHK_PTR_NULL(sqId);

    rtError_t ret = rtStreamGetSqid(stm, sqId);
    HCCL_DEBUG("Call rtStreamGetSqid stm:%p, sqId:%u value[%d].", stm, *sqId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtStreamGetSqid]rtStream Get Sqid failed stm:%p, "\
        "sqId:%u value[%d].", stm, *sqId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtStreamGetSqid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtStreamGetCqid(const rtStream_t stm, uint32_t *cqId, uint32_t *logicCqId)
{
#ifndef HCCD
    CHK_PTR_NULL(stm);
    CHK_PTR_NULL(cqId);
    CHK_PTR_NULL(logicCqId);

    rtError_t ret = rtStreamGetCqid(stm, cqId, logicCqId);
    HCCL_DEBUG("Call rtStreamGetCqid stm:%p, cqId:%u, logicCqId:%u value[%d].", stm, *cqId, *logicCqId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtStreamGetCqid]rtStream Get logicCqId failed stm:%p, " \
        "cqId:%u logicCqId:%u value[%d].", stm, *cqId, *logicCqId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtStreamGetCqid]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtResourceClean(int32_t devId, rtIdType_t type)
{
#ifndef HCCD

    rtError_t ret = rtResourceClean(devId, type);
    HCCL_DEBUG("Call rtResourceClean devId: %d, ret: %d", devId, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtResourceClean]rtResourceClean failed devId:%d, "\
        "return value[%d].", devId, ret), HCCL_E_RUNTIME);
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[rtResourceClean]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtGetHccsPortNum(u32 deviceLogicId, s32 &num)
{
#ifndef HCCD
    constexpr int32_t moduleType = DEV_MODULE_TYPE::MODULE_TYPE_SYSTEM;
    constexpr int32_t infoType = DEV_INFO_TYPE::INFO_TYPE_MAINBOARD_ID;
    constexpr s32 HCCS_PORT_NUM_UNKNOWN = -1;
    constexpr s32 HCCS_PORT_NUM_910_93_UNKNOWN = -1;
    constexpr s32 HCCS_PORT_NUM_910_93_6 = 6;
    constexpr s32 HCCS_PORT_NUM_910_93_7 = 7;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V1_2_4_4 = 0x1c;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V1_4_8_8 = 0x1d;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V2_4_8_7 = 0x18;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V2_2_8_7 = 0x19;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V3_4_8_7 = 0x14;
    constexpr int64_t MAINBORDID_910_93_PRODUCT_V3_2_8_7 = 0x15;
    DevType deviceType;
    CHK_RET(hrtGetDeviceType(deviceType));
    if (deviceType != DevType::DEV_TYPE_910_93) {
        num = HCCS_PORT_NUM_UNKNOWN;
        HCCL_WARNING("[hrtGetHccsPortNum]deviceType: %d, does not support this interface.", deviceType);
        return HCCL_E_NOT_SUPPORT;
    }
    int64_t val = 0;
    rtError_t ret = rtGetDeviceInfo(deviceLogicId, moduleType, infoType, &val);
    HCCL_DEBUG("Call rtGetDeviceInfo deviceLogicId:%u, val: %lld, ret:%d.", deviceLogicId, val, ret);
    CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[rtGetHccsPortNum]rtGet Hccs Port Num failed deviceLogicId:%u, "
        "return value[%d].", deviceLogicId, ret), HCCL_E_RUNTIME);
    switch (val) {
        case MAINBORDID_910_93_PRODUCT_V1_2_4_4: num = HCCS_PORT_NUM_910_93_6; break;
        case MAINBORDID_910_93_PRODUCT_V1_4_8_8: num = HCCS_PORT_NUM_910_93_6; break;
        case MAINBORDID_910_93_PRODUCT_V2_4_8_7: num = HCCS_PORT_NUM_910_93_7; break;
        case MAINBORDID_910_93_PRODUCT_V2_2_8_7: num = HCCS_PORT_NUM_910_93_7; break;
        case MAINBORDID_910_93_PRODUCT_V3_4_8_7: num = HCCS_PORT_NUM_910_93_7; break;
        case MAINBORDID_910_93_PRODUCT_V3_2_8_7: num = HCCS_PORT_NUM_910_93_7; break;
        default:    num = HCCS_PORT_NUM_910_93_UNKNOWN;
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtGetHccsPortNum]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

HcclResult hrtThreadExchangeCaptureMode(rtStreamCaptureMode *mode)
{
#ifndef HCCD
    HCCL_DEBUG("Call rtThreadExchangeCaptureMode mode before: %d", *mode);
    rtError_t ret = rtThreadExchangeCaptureMode(mode);
    HCCL_DEBUG("Call rtThreadExchangeCaptureMode mode after: %d, ret: %d", *mode, ret);
    if (ret == RT_NOT_SUPPORT) {
        HCCL_INFO("[hrtThreadExchangeCaptureMode]rtThreadExchangeCaptureMode not support!");
        return HCCL_E_NOT_SUPPORT;
    } else {
        CHK_PRT_RET(ret != RT_ERROR_NONE, HCCL_ERROR("[hrtThreadExchangeCaptureMode]rtThreadExchangeCaptureMode failed mode:%d, "\
            "return value[%d].", *mode, ret), HCCL_E_RUNTIME);
    }
    return HCCL_SUCCESS;
#else
    HCCL_ERROR("[hrtThreadExchangeCaptureMode]Does not support this interface.");
    return HCCL_E_NOT_SUPPORT;
#endif
}

__attribute__((constructor)) void CallBackInitRts()
{
    g_deviceType = DevType::DEV_TYPE_COUNT;
    g_deviceLogicId = INVALID_INT;
    g_devicePhyId = INVALID_UINT;
    HCCL_RUN_INFO("[adapter_rts.cc][CallBackInitRts] g_deviceType [%d] g_deviceLogicId [%d] g_devicePhyId [%d]",
        g_deviceType, g_deviceLogicId, g_devicePhyId);
}
